William W
William W

Reputation: 1806

window.location truncating URL with button onclick

When setting window.location with a button onclick event, the result is being truncated after the second parameter name.

This code:

<button onclick="window.location='index.php?p=reports_manage&id=new'">create new report - button</button>

Is sending the the user to this page:

https://foo.com/index.php?p=reports_manage&id=

Even if I add extra parameters at the end it still gets cut off at the same place.

All of these work fine however:

<a href="index.php?p=reports_manage&id=new">create new report - link</a>
<a href="javascript:window.location='index.php?p=reports_manage&id=new'">create new report - JS1</a>
<a href="javascript:void(0);" onclick="window.location='index.php?p=reports_manage&id=new'">create new report - JS2</a>

Any idea what could be causing this? As far as I can tell the button was working fine earlier and broke without any change to this code. What else could be affecting this? There are no javascript errors on the page and it happens regardless of what browser is used.

Upvotes: 2

Views: 20306

Answers (2)

trickyzter
trickyzter

Reputation: 1591

A long shot, but maybe specifying the button type will help. In this case type="button".

@William: Different browsers have different pre-requisites, however as a safeguard it's always good to be explicit. ;)

Upvotes: 4

GillesC
GillesC

Reputation: 10874

Use <input type="button" /> instead of <button></button> as it has unexpected behaviour (and if use to submit a form it will even submit its content!)

Upvotes: 3

Related Questions