Forum Moderators: open

Message Too Old, No Replies

simple location script not working in Firefox?

javascript firefox

         

WEBenny

2:35 pm on Feb 26, 2006 (gmt 0)

10+ Year Member



Hi

I hope someone can give me a clue about why this is happening. I'm no expert in Javascript.

I have this simple code that loads a new page when a button is clicked.

---
function emptybasket() {
document.location='emptybasket.php';
alert("Shopping basket empty!");
}
---

The last alert line was added because Firefox ignores the document.location line without having this line or I guess a similar one.

I.E works fine without the alert line, if anyone has a clue why this is happening please let me know.

Best regards

Benny

Bernard Marx

4:00 pm on Feb 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think document.location is deprecated. You should also be assigning to the
href
property.

The global (window) location object is more reliable.

window.location.href = "...."

[edited by: DrDoc at 6:35 pm (utc) on Feb. 26, 2006]
[edit reason] code fix [/edit]

WEBenny

8:39 pm on Feb 26, 2006 (gmt 0)

10+ Year Member



Hi Bernard

Thanks for your suggestion. I just tried it:

Changed the script to this:

---
function emptybasket() {
window.location.href='emptybasket.php';
}
---

As the former it works fine in IE, but not in Firefox?
Adding the following line makes the redirect work.

alert("Now it works...");

But that is highly annoying and I am very interested in knowing what causes this problem?

Further information that I might suspect causing the problem are that the page is located in a frame. And the redirect works if I set the location to the top level.

Best regards

Benny

WEBenny

6:36 pm on Mar 3, 2006 (gmt 0)

10+ Year Member



Bump, anyone has a clue?

Best regards

Benny

Bernard Marx

6:54 pm on Mar 3, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry. I'm lost on that one.

kaled

7:42 pm on Mar 3, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could try location.replace('emptybasket.php');
This would overwrite the existing page in the history object.

However, my guess would be that there is some other js code on the page (or other frame) that is interfering somehow.

As an alternative you could use an html link (with an image of a button). This has the advantage that javascript is not required (but I guess other features require it).

If you use a link, consider adding rel="nofollow" to keep search engines out.

Kaled.

WEBenny

9:13 pm on Mar 15, 2006 (gmt 0)

10+ Year Member



Hi all

Thanks for your suggestions.

I finally managed to find out why it was not working or rather what was causing it not to work.

I had a button with OnClick="emptybasket();" action. within a form.

After changing the <button> to the form element input type="button" it worked?

Remember this worked fine in IE but not in Firefox and I did make several w3 validations on the webpage without getting any errors.

Now if anyone could tell me why Firefox does not permit a <button> within a form when you try to use location within a frame?

Best regards

Benny