简体   繁体   中英

Browser is not redirecting but url is changing

I want to redirect my page by clicking on entire row.

For that my HTML table row is below which is created by javascript.

id = 1;
<tr onclick="document.location.href='http://localhost/emailc/emailCampaignEdit/"+id+"'">
    <td><b>abc</b></td>
</tr>

before clicking on above code my browser's url is http://localhost/emailc

After click on code I want to redirect my page on this url http://localhost/emailc/emailCampaignEdit/1

Browsers url is changes perfectly on clicking. but browser is not redirecting to that url. If I refresh browser on that url then it works as I want.

I have tried this also for testing the syntax:

<tr onclick="document.location.href='http://www.google.com'">
    <td><b>abc</b></td>
</tr>

It works perfectly, browser is completely redirecting to google page and I have also tested other redirecting codes likes window.location.href or window.location.assign or window.location everything has same problem.

<tr onclick="openWin('1')">
    <td><b>abc</b></td>
</tr>
<script>
function openWin(id)
{
window.open("http://localhost/emailc/emailCampaignEdit/"+id)
}
</script>

can you try this out ?

Edit :

you may use this function :

location.reload(false);

which will reload page from server , instead reloading page from cache , right after the url changes

so you should use :

function openWin(id)
    {
    window.open("http://localhost/emailc/emailCampaignEdit/"+id,"_self");
    location.reload(false);
    }
var id = 1;
document.write('<table>'+
'<tr onclick="document.location.href=\'http://localhost/emailc/emailCampaignEdit'+id+'\'">'+
'<td><b>abc</b></td>'+
'</tr></table>');

Please try above code

this should work, it works for me

var td = document.getElementsByTagName('td'), id = 1;

td[0].addEventListener('click', function(){location.assign('yourURL.php?id=' + id);}, false);

Note that document.location.href returns the URL of the page and does not redirect. Refer to http://www.w3schools.com/jsref/prop_loc_href.asp for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM