Discussion Locked

Javascript IF statement

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I am looking for code to employee a certain if statement. This will be in the head section of the html document and I need it to do this:

if ("page link that has been clicked" does not contain "www.ourorg.org" somewhere in the link)

msgbox("disclaimer, you are leaving our stuff adn going to someone else's stuff so don't hold us accountable. Bye")

end if

The only part that I need help with is that if paragraph. I don't know what thing I would use to indicate if a link is clickedand I don't know how to tell it to check the link for a certain string's partial existance.

Any help?

5 replies

by jay eckles

Well, one obvious solution is to put the code to launch the disclaimer in the onClick event of the external hyperlink:

You're using the onClick event for sure since you're catch the event where the user has clicked on a link, so you might as well put the code there, or at least a call to a function that contains the code.

If you really want to use your if statement and really search the string of the hyperreference, you could enclose the hyperlink in a SPAN element, then use the innerHTML property of the SPAN to get the complete anchor tag (including the URL in the HREF). Then do an indexOf on that string and if it's < 0, your string isn't there...


function myfunction( spanid ){
atag = document.all[spanid].innerHTML ;
if( indexOf( atag, "www.ourorg.org" ) < 0 ){
alert( "disclaimer, you are leaving our stuff adn going to someone else's stuff so don't hold us accountable. Bye" ) ;
}
}

Note that I haven't tested any of the code samples above, but they should be enough to get you going in the right direction.

by peyison

One way to do this is to create a JavaScript function that is called by each link. The function would check for the existance of the URL and display the message when needed. Something like:

JavaScript function in the section:

--------------------------------

Then the links would look like:

Link to Microsoft
Link to stuff site

Note that in the function, the URL to search for is surrounded by /'s, not "'s. The i at the end is for case-insensitive matching. (This uses Regular Expressions).

Hope this helps.

This discussion is closed to new replies.