JPatrickDev
JPatrickDev

Reputation: 95

window location href not working

I am trying to navigate to a new page using javascript. When I put window.location.href = "next.html" in the header of a page it works fine. but when calling it from a function after a button is clicked, it does not work. Example code:

function next(){
 $post = "?id=10"
        alert($post);
     window.location.href = "next.html" + $post;
 }

the alert displays ?id=10, but it does not redirect. Thanks for the help

The " missing was a typo and is not in the real site, sorry!

Upvotes: 1

Views: 3259

Answers (2)

Sir
Sir

Reputation: 8280

This:

function next(){
 $post = "?id=10
        alert($post);
     window.location.href = "next.html" + $post;
 }

Is a syntax error in your PHP. Try this:

<?php
    function next(){
     $post = "?id=10";
?>
     alert($post);
     window.location = "next.html" + <?php echo $post; ?>;
<?php
    }
?>

You really should not mix PHP with JavaScript like this how ever.


EDIT: Ok after confusion has been cleared - if no PHP is involved try:

function next(){
 var post = "?id=10";
        alert(post);
 window.location = "next.html" + post;
 }

Upvotes: 5

sv_in
sv_in

Reputation: 14039

Seems like there is a error in the code:

$post = "?id=10 should have " in the end

Upvotes: 0

Related Questions