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
 
What line are you getting it on?

Also, download Firefox. It has a significantly superior Javascript error console.

--Chessbot

There is a level of Hell reserved for probability theorists in which every monkey that types on a typewriter produces a Shakespearean sonnet.
 
and while you're at it, rename your variable...

*cLFlaVA
----------------------------
[tt]insert funny quotation here.[/tt]
 
Yes. Good catch.

--Chessbot

There is a level of Hell reserved for probability theorists in which every monkey that types on a typewriter produces a Shakespearean sonnet.
 
Ok...now I have:

browser_referrer = document.referrer;

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

alert(browser_referrer);

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

The 2 lines that I have commented out are the ones that I am having problems with. The error I get in Firefox for the line of:

browser_referrer = browser_referrer.replace(/\//gi, "_");

is "missing ) after argument list".

Thanks!
Ben
 
It is apparently having problems with the escaped foreslash. Try double-escaping it:
Code:
browser_referrer = browser_referrer.replace(/\\\/gi, "_");
Experiment with different numbers of backslashes and see which it works on.

--Chessbot

There is a level of Hell reserved for probability theorists in which every monkey that types on a typewriter produces a Shakespearean sonnet.
 
Chessbot,

That is an interesting idea for a solution. I used to have:

browser_referrer = browser_referrer.replace(/\\//gi, "_");

which usually seemed to work. However, then I noticed when a visitor comes from a few referring sites, I get a javascript permission denied error.

Thanks!
Ben
 
How would Javascript produce a permission denied error? What were the sites?

(Oh, and try using
Code:
browser_url = location.href.split("?")[1];
instead of
Code:
browser_url = document.URL.split("?")[1];
)

--Chessbot

There is a level of Hell reserved for probability theorists in which every monkey that types on a typewriter produces a Shakespearean sonnet.
 
Also, I only get the permission denied error in IE not in Firefox, but I need the code to work in IE too.

Thanks!
Ben
 
What do you mean by a permission denied error? When trying to change the variable? Or accessing the page?

--Chessbot

There is a level of Hell reserved for probability theorists in which every monkey that types on a typewriter produces a Shakespearean sonnet.
 
Yeah, I'm not sure what line of code the page is giving the error for. Basically, if I go to a few other pages that have links to my site and then click my link, I get a permission denied error. I have code that gets the referring site info and the current browser url. Is there some way that sites can block you from getting their browser referring info or something?

Thanks!
Ben
 
I'm sure there is, I'm just not sure how...

--Chessbot

There is a level of Hell reserved for probability theorists in which every monkey that types on a typewriter produces a Shakespearean sonnet.
 
yeah, my code works perfect when clicking the link to my site on the referring site when clicking the link in Firefox but I get a permission denied error when doing it through IE.

Thanks!
Ben
 
Let me try to explain the problem a little better. There are a few websites that contain a link to my site. When I click my link on one of those sites in IE, I get a permission denied error. My javascript code is used to get the referring url from the browser. The code works perfect with most sites in IE and all sites in Firefox, but I can't figure out why I am getting this permission denied error with a few referring sites in IE. My javascript code line that seems to be causing the problem is:

browser_referrer = document.referrer;

Thanks!
Ben
 
That means that, for whatever reason, some security system on the browser is preventing you from getting the referrer. There is no way to get around this that I can think of; you will just have to make do.

--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)
 
if you're still not satisfied, you could always just try a simple alert(document.referrer) to see what happens.

*cLFlaVA
----------------------------
[tt]Sigs cause cancer.[/tt]
 
I was just noticing that it works on one of my computers using windows 98, so maybe it has something to do with Windows XP or SP2, but why just with a few websites?

Thanks!
Ben
 
I haven't found any documentation explaining permission settings. If the referrer was a secure page, or if there was no referrer, it should return an empty string, not an error.

[«] read more [»]

*cLFlaVA
----------------------------
[tt]Sigs cause cancer.[/tt]
 
I tried alert(document.referrer) and got the permission denied error for that line. Is there a different way to write document.referrer?

Thanks!
Ben
 
no.

*cLFlaVA
----------------------------
[tt]Sigs cause cancer.[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top