Link to home
Start Free TrialLog in
Avatar of eaweb
eawebFlag for undefined

asked on

location.replace javascript

hi,

i have this link wich is using location.replace but it is not working:

<a href="errrep.asp?TRANSEQ=<%= Request.Querystring("transeq") %>" onclick="location.replace('itmAl.asp');"><img src="../images/ErrorReports_a.gif" border="0" /></a>

the itmAl.asp is a navigation error page that is suppose to go to the browser history instead of errrep.asp and is suppose to come up when hitting the browser back button.

may be i have some syntax error or isn't it suppose to work like this?????
Avatar of eaweb
eaweb
Flag of undefined image

ASKER

i want to also use location.replace when submitting form. is that possible??? how must i set it up????
Avatar of hielo
I think you meant:
onclick="location.href ='itmAl.asp';">
BTW: The replace method takes two arguments: the first is the string or expression to search for, and the second is the "thing" to replace it with. So, yes , you were using it incorrectly. It also does not do inline replace.
So if I have this:
 var x = "Good";
simply doing:
x.replace("G","M");

will NOT replace change the value of x to Mood. If you wanted to do that, you would need to "catch" the returned value of the replace:
x = x.replace("G","M");

In case it was not clear, this is what I was suggesting:
<a href="errrep.asp?TRANSEQ=<%= Request.Querystring("transeq") %>" onclick="location.href='itmAl.asp'"><img src="../images/ErrorReports_a.gif" border="0" /></a>
Avatar of itjstagame
itjstagame

The replace function is different for location and for string.  location.replace('URL') is valid syntax but it will not add a new entry into the IE History so you cannot click back and get to the page you'll be calling itmAl.asp from.

You want location.href= as Sage has suggested, this enters an entry in the History list.
ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oh, I didn't see that's what you were trying to do.  Yes, use errrep to switch the pages and make sure if you use a page like that to use location.replace because that will prevent errrep.asp from being added to the history.  If you used location.href= there then errrep.asp would be added to the history and hitting 'Back' would give the annoying error of constantly forwarding you again (I'm sure we've all experienced sites like that).
Avatar of eaweb

ASKER

hi,

i solve it this way:

<script type="text/javascript">
window.onbeforeunload = function() {
      window.location.replace('pageC.asp');
     
}
</script>
That looks like a recipe for disaster...