...

View Full Version : string match help



pyroman131
12-27-2005, 06:11 PM
I'm trying to create a script where all the hyperlinks in a page will be replaced with a new set of coding before it. (e.g: a href="this.htm?http://www.google.com").

My code generates this change through a series of random numbers and only runs the script when "2" is the generated number (out of 12). Now, I only know how to replace all occurrences of "href" in all "a" tags, but there are times when I use Javascript to launch new windows or even javascript to navigate to the top of a page.

I tried to use a string match method to search for occurrences of "javascript:" and so forth, but I can't seem to get it working.

Here's what I got so far:

function loadInterstitial() {
var ranNum = Math.round(Math.random()*11);
var adUrl = "ad.htm";

if (ranNum == 2) {
var links = document.getElementsByTagName("a");

for (var i=0; i<links.length; i++) {
var redir = links[i].href;
var JScript = links.match("javascript:");

if (JScript = true) {
links[i].href = redir;
} else {
links[i].href = adUrl + "?redir=" + redir + "&timer=3000";
}
} else if (ranNum != 2) {
return ranNum;
}
}
I'm trying to get a page to load in between browsing (such as an interstitial, only not as annoying). I can make it appear for every link fine and randomize it as I please, but I can't seem to get this part to work.

Problem: I don't want the script to load the new url when I'm using javascript to launch new windows in "a" tags. (e.g: a href="javascript:scroll(0,0)" is changed to a href="ad.htm?javascript:scroll(0,0)"). Is there any way around this?

I tried using external Javascript files to write out the code "a href='javascript:scroll(0,0)", but this script still detects it and writes the code before this link. I don't want to use IFRAMEs, because they are hard to manipulate with my site.

Solution, please?

konithomimo
12-27-2005, 07:16 PM
The reason your code wont work is because you have:

if (JScript = true)

That is assigning the value of "true" to JScript.It should be:

if (JScript == true)

Another way to do it is to give your HREFs classes. Then inside your script check the class of the HREF chosen. If it is one that you don't want to change then do nothing. If it isn't then do whatever you want.

function loadInterstitial() {
var ranNum = Math.round(Math.random()*11);
var adUrl = "ad.htm";

if (ranNum == 2) {
var links = document.getElementsByTagName("a");

for (var i=0; i<links.length; i++) {
var redir = links[i].href;
if (links[i].className == "JS") {
links[i].href = redir;
} else {
links[i].href = adUrl + "?redir=" + redir + "&timer=3000";
}
} else if (ranNum != 2) {
return ranNum;
}
}

pyroman131
12-28-2005, 04:44 AM
Okay, I tried it the first method by setting it to "== true", but that didn't work. Instead, I set it as a class name in order to achieve the best effect. And it worked!

Thank you very much for your speedy help.

konithomimo
12-28-2005, 04:05 PM
Glad to have been of help.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum