Kamala Harris - Internet Advertising - Debt Consolidation - Web Hosting - Valentine Gifts For Him

PDA

View Full Version : JavaScript function "if there is url in document"


Sordello
May 3rd 2009, 8:08 pm
Hi,

I need a JavaScript function tha does that:

* Verify if there is an URL in document
* If there is so ok.
* If not shows an alert.

Can anyone help me?

camjohnson95
May 3rd 2009, 10:57 pm
If you mean within the current document something like this:

<script type="text/javascript">
window.onload = function() {
if(document.body.innerHTML.search("http://www.yahoo.com") == -1) window.alert("URL Could not be found in document.");
}
</script>


You have to make sure that the script is inserted BEFORE the <body> tag of the document....

Sordello
May 3rd 2009, 11:10 pm
Hi Camjohnson95,

I tried adding code before <body> and it worked perfectly.

But there is any way to work inside <body> tag?

Regards and thank you very much,

Diego

camjohnson95
May 4th 2009, 1:35 am
ok,
The reason that it needs to be before the body tag is because otherwise it picks up the string from the script and always return true... so if we join two parts of the url to make one it will work fine from anywhere:


<script type="text/javascript">
var strUrl;
window.onload = function() {
strUrl = "http://".concat("www.yahoo.com");
if(document.body.innerHTML.search(strUrl) == -1) window.alert("URL Could not be found in document.");
}
</script>