PDA

View Full Version : stuck in page...any help?



pivey
09-10-2005, 02:25 PM
Hey to all!
I really need your help here, could not find any solution.

I have a product index
-> user selects item
-> detail page xxx.htm loads and a script is loading Variants & Category ID's for this url). The loaded htm looks now like xxx.htm?categoryId=45
What actually happens is, that page is loading twice to browser's history

->now, user views the product detail page, he can put the item to shopping cart..and will be redirected to the orderform.htm.
-> Or...user wants to navigate back....but he stucks at the current page!!

I have added a back button with history.go(-2), works fine....
but how can I fix the bug if the user (and he will) uses the browser back button?

You may see it at:
http://www.12voltshop.gr/pi-1517753331.htm
(see how categories are loaded when entering the page)

I need a command that executes onunload:

Only
if (the user uses browser back button)
history.go(-2)
else (if, he chooses to put the item to shopping cart ->exetute the script for that action

or

The other solution would be not to run the script wchich loads variants & ID's when navigating one(1) history back.

Help is appreciated.

Dimi

Twey
09-10-2005, 03:36 PM
Use location.href.replace(url). This will prevent the first page from being added to the history.

pivey
09-10-2005, 04:32 PM
Hello Twey..thank you.
I have done this. It does not work. Strange...don't know how to explain but in fact it is the same page....so if I use this command the page tries to reload...reload...reload

Know what I mean?

I need a function which "sees" that the browser goes 1 back...so the history should be (-2) in that case.
Else...if the user selcts the item to place it to the shopping cart->execute addToBag()

jscheuer1
09-10-2005, 04:55 PM
It can and perhaps should be:

location.replace('url')

But, more importantly is how did you use this? If used in a script that runs each time the page is loaded, a test must be made to be to see if the query string has already been added or not. Since you have a lot going on in both pages, show us the exact script where the query string is added, highlighting the code that does it.

pivey
09-10-2005, 07:42 PM
Hello John!
The script is executed every time a page is loaded:
var urlstring="defaultVariants=";
var baseurl=location.href.split("?");
var jx=location.href.search(/&/);
if (proComb[0][5]!="" && jx==-1){
for (var i=5;i<proComb[0].length;i++){
urlstring+='search'+(i-5)+'_EQ_'+proComb[0][i]+'_AND_';
}
urlstring+='%7BEOL%7D';
location.href=baseurl[0]+'?'+urlstring+'&categoryId='+(Product.EANCode);
}
else if(location.href.search(/=/)==-1) location.href=baseurl[0]+'?categoryId='+Product.EANCode;


Well, I found a way(i you can say so) that now does return to the product index using
<script>
var mypageback = "pi644791070.htm"
var i=true;
</script>

<body onunload='if(i) window.location=mypageback'>

Everything works fine, back with browser, AddToBag, Refresh....but the problem now is that if you click any other link (like product recommendation, help desk etc.) , even then the page returns to the same pi644791070.htm

I have placed a product for your inspection at
http://www.12voltshop.gr/pi644791070.htm the first item "Alphasonik PSW 612"

Dimi

jscheuer1
09-10-2005, 10:16 PM
That's brilliant! Now all you need to do is, in the links that you do not want to go to pi644791070.htm, put this:


onclick="i=0;return true;"

pivey
09-11-2005, 10:24 AM
John , you are right!
Solving this would be onClick="i=false;" as well

But since the other links are genaretad autom. it is a lot of work to do afterwards for all pages to add onClick....

How can I have a script (maybe based on the one who does load variants...)which does:
ONLY if the user goes back to previous page ->run onunload='if(i) window.location=mypageback'
ELSE: follow other links as they are

Thanks
Dimi

Twey
09-11-2005, 01:14 PM
onclick="i=false;" would not work. The = operator, as well as assigning the value to the right, also returns that value. This would be the same as "i=false;return false;" The link will not work.

I'm not sure if you can do what you requested above. Something with cookies, perhaps?

jscheuer1
09-11-2005, 03:27 PM
First of all, I'm not sure what Twey is saying but, in any case, adding:

onclick="i=0;return true;"

or:

onclick="i=false;return true;"

to a link will still allow the link to fire when clicked. That out of the way, we could add to the onload event(s) that fire for the page, language that adds this onclick event only to the links we desire. Something like this:

<script type="text/javascript">
function addTrue(){
var links=document.getElementsByTagName('a')
for (var i_tem = 0; i_tem < links.length; i_tem++)
if (links[i_tem].href.indexOf('pi644791070.htm')==-1&&links[i_tem].href.indexOf('javascript')==-1)
links[i_tem].onclick=function(){i=0;return true;}
}
</script>Still, if possible, it would be better to catch the query string addition and reload and prevent it from happening a second time. This was my first idea, something like:

if (location.href.indexOf('?')!==-1)
return;added to your function that adds the query string and reloads the page in the first place. Then, also employing the location.replace() method should not cause the multiple reloads. I'm having trouble following your code for that and perhaps '?' would not be the best character to test for but, there should be something we could use.

pivey
09-11-2005, 04:15 PM
Hello! John you are right.
I used this one:
<script>
function setLinks(){
var lnk = document.links;
for(var n=0;n<lnk.length;n++){
lnk[n].onclick = function(){i=false};
}
}
</script>

<body onload="setLinks()" onunload='if(i) window.location=mypageback'>

Well, it works...except some deep links (like bundled item links) but I don't worry about that 'cause I want to change them to _blank windows anyway.

Your script sounds good, the prob is that I will have to do this for 1600-2000 pages.
However to catch the query string addition has to be the most advanced solution for that.
But that means that the script must recognize the final URL to be built uppon load.....hmmm I can't figure it out.
But it works now!
If you have an idea I would be glad to hear it.

Dimi

Twey
09-11-2005, 07:04 PM
I'm not sure what Twey is saying
i=false as the last and only statement in an onclick would return false and stop the link working.

mwinter
09-11-2005, 07:14 PM
i=false as the last and only statement in an onclick would return false and stop the link working.No, it wouldn't.

You are quite right in saying that an assignment operator evaluates to the right-hand expression, but that is far from the same as returning that value.

Event listeners, whether created via a script or included in markup, are functions. Placing an expression at the end of a function doesn't result in the return of that expression, so neither will it happen with listeners. If you are observing it with a particular browser, then that is an isolated case and should be reported as a bug.

Mike

Twey
09-11-2005, 07:22 PM
Hm?
If you use a function for onclick, the return value of that function will be passed to the event (I believe).
E.G.


<script type="text/javascript">
function fail() { return false; }
</script>
<a href="page" onclick="fail();">Clicking here will do nothing</a>

I always thought that the return value of the last statement was used as the return value (E.G. the above would have the same effect as:
<a href="page" onclick="return false;">Clicking here will do nothing</a>).

mwinter
09-11-2005, 07:32 PM
If you use a function for onclick, the return value of that function will be passed to the event (I believe).No. You'd have to explicitly return that return value.

An attribute like:


<a id="myLink" ... onclick="/* some code */">is functionally identical[1] to:


document.getElementById('myLink').onclick = function(event) {
/* some code */
};The difference is that the browser performs this translation for you, and does it as it parses the markup.

As you should know, if there is no return statement in a function, the function call evaluates to undefined. Though undefined type-converts to false, this isn't sufficient; the value must be explicitly false to cancel an event.

Mike


[1] Except in IE where there is no event argument to the listener: the event object is, of course, global.

pivey
09-11-2005, 08:59 PM
<script>
var mypageback = "BACK TO PREVIOUS HTM"
var i=true;
</script>
<body onunload='if(i) window.location=mypageback'>

<a href="ANY OTHER LINK ON LOADED HTM" onClick="i=false;">to any other page </a>

Back, forward, refresh, to any other link.....Works fine for me.