I'm trying to write a demo page for my site that works completely on AJAX. What I mean by this is that the page will stay the same, but load new pages by changing the <BODY> of the document.

I'm writing a script that will take every link and decide whether to add the attribute target="_blank" or onClick="getPageWithAJAX();" Right now I'm writing the regular expression for it, which is working perfectly in the Regex tester.

The problem is, it won't return a match when I put only "index.php" for the URL.

Here is my coding: ajax.js


function getPage(url) {
			var domain = new RegExp("/(" + window.location.host + ")|(^\/.*)|(^[\w\d]*\.[\w\d]{2,})/", "i");
			if (domain.test(url))
				alert(url + ": Same domain");
				alert(url + ": Different domains");

I included the alert() s so that I can see if the match was made or not, for debugging purposes. home.php

	<title>AJAX Website</title>
<script type="text/javascript" src="ajax.js">
// Copyright (c) 2009 Wurbo
// Use only with explicit permission from Wurbo

<body onLoad="getPage('http://example.com');getPage('/testFolder/index.php');getPage('index.php');getPage('http://google.com');">


This should alert: Different domains Same domain Same domain Different domains But on the third one, it's giving me "different domain". Anybody know why?

9 Years
Discussion Span
Last Post by itsjareds

Figured it out on my own, I forgot to add double escape slashes since I was inserting the RegExp into the RegExp object.

Here is my JavaScript regexp variable now:

var domain = new RegExp("(" + window.location.host + ")|(^\\/.*)|(^[\\w\\d]*\\.[\\w\\d]{2,})", "i");
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.