Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript Replace Command Error

Status
Not open for further replies.

WebStuck

Programmer
Apr 12, 2003
79
US
Hi,

I am using IE 6 with script debugging enabled. I am getting an " Expected ')' " error from my following lines of code:

referrer = document.referrer;

referrer = referrer.replace(/http:\/\//gi, "");
referrer = referrer.replace(/\.asp/gi, "_asp");
referrer = referrer.replace(/\.cgi/gi, "_cgi");
referrer = referrer.replace(/\.php/gi, "_php");
referrer = referrer.replace(/\.pl/gi, "_pl");
referrer = referrer.replace(/\//gi, "_");
referrer = referrer.replace(/-/gi, "_");

url = document.URL.split("?")[1];

Could you please let me know how I can fix this error?

Thanks!
Ben Cunningham
 
I just tried to put if (document.referrer) and got a permission denied error for that line. For some reason if I have document.referrer anywhere, I get a permission denied error. I believe there has to be a way around causing this error, even if I can't end up getting the referrer information.

Thanks!
Ben
 
Sure. Just remove all reference to document.referrer and you shouldn't get a permission denied error. This property is apparently suppressed by browsers for security issues.

--Chessbot

"In that blessed region of Four Dimensions, shall we linger on the threshold of the Fifth, and not enter therein? Ah, no! [...] Then, yielding to our intellectual onset, the gates of the Sixth Dimension shall fly open; after that a Seventh, and then an Eighth -- --" Flatland, A. Square (E. A. Abbott)
 
No, I want to use it because it works fine on most sites just not a few.

Thanks!
Ben
 
So use it and put this in your code (for error suppressing):
Code:
function err()
{
  return false;
}
window.onerror = err;

--Chessbot

"In that blessed region of Four Dimensions, shall we linger on the threshold of the Fifth, and not enter therein? Ah, no! [...] Then, yielding to our intellectual onset, the gates of the Sixth Dimension shall fly open; after that a Seventh, and then an Eighth -- --" Flatland, A. Square (E. A. Abbott)
 
Great!
A caution though: With this function, all errors will be suppressed, so only use it if you are sure that the script is completely debugged.

--Chessbot

"In that blessed region of Four Dimensions, shall we linger on the threshold of the Fifth, and not enter therein? Ah, no! [...] Then, yielding to our intellectual onset, the gates of the Sixth Dimension shall fly open; after that a Seventh, and then an Eighth -- --" Flatland, A. Square (E. A. Abbott)
 
Why not use the try/catch method, so you're only suppressing this error?

Code:
var dr;

try {
    dr = document.referrer;
} catch (e) {
    dr = '';
}

*cLFlaVA
----------------------------
[tt]Sigs cause cancer.[/tt]
 
Yeah, I tried using try and catch. Unfortunately, the error cames up anywhere I use the words document.referrer. I don't want to have to put try and catch everywhere that I type document.referrer so the onerror code works better.

Thanks!
Ben
 
You don't need to modify all the places you use the referrer. Just put some code at the top of the entry routine.

In that code, grab the referrer and put it into a named variable -- with that error-trapping code on. Inside the error-trapping code (which would only execute when you get the access error), assign a null string to the named variable.

So from that point on, you only use this other variable. Since any error condition would've set it to null, you can safely use it anytime from that point on, checking for null if need be.

That way you can use the try/catch once but still use the value anywhere in your code -- and not turn off error checking globally (never a good thing).

Good luck. MERRY CHRISTMAS.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top