Вопрос

I have a script that gets throught parameter the new page andthe redirects to this page. my function:

function openPage(page) {
    alert("Pressed the button!"); //working
    window.location.href = "http://localhost:8080/Edas/" + page; //not working
}

and my button:

<button id="btnViewMonthlyPurchaseReport" onclick="openPage('monthlyPurchaseReport.html')">View Monthly Purchase Report</button>

I think my problem is in my base URL ("localhost:8080/Edas/"), but really don't know how to fix it. Thanks!!

Это было полезно?

Решение

Out of the comment the problem was that the button was inside of a form element.

The problem is that a button without a type attribute will submit the form, which would (not sure if in every browser) result into the described problem, that the window.location.href = ... has no effect and that the page would be redirected to the url defined in action. If action is not defined in the form then it is the the current url.

You have different options to solve this:

  1. You could move the button out of the form (if it would not affect usability)
  2. Change the type of the button to button (<button type="button">View Monthly Purchase</button>) - the default behavior without a type is submit
  3. You could place a return false; at the end of your openPage function to prevent the default behavior (this is similar to 2. but with the difference that the button would still be marked as a submit button which could be important for usability in some situations).

Другие советы

Do this:

JS

var url = window.location.href;
var dirnames = url.split('/');
function openPage(page) {
   alert("Pressed the button!"); //working
   window.location.href = "http://"+ dirnames[2] +"/Edas/" + page; 
}

I imagine you're correct in saying that your issue is in the URL.

Where is your monthlyPurchaseReport.html? If it is in the same directory as where the html with your button is, simple use:

function openPage(page) {
   alert("Pressed the button!"); 
   window.location.href = page; 
}

My problem was: my button was wrongly inside a form, which @t.niese clearly explained why. Thank you all for the help!

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top