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

alert(window.location.host);

function getPage(url) {
			var domain = new RegExp("/(" + window.location.host + ")|(^\/.*)|(^[\w\d]*\.[\w\d]{2,})/", "i");
			if (domain.test(url))
				alert(url + ": Same domain");
			else
				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

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

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

</body>
</html>

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?

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.