[ACCEPTED]-Create and go to url with Javascript-javascript

Accepted answer
Score: 29

Simply try:

window.location = url;

But before trying to do that, you 15 have to make sure the page at the address 14 "http://" + hostAddress "/" + triggerNumber exists. For example by putting valid triggerNumbers 13 in an array and check if it exists or not. So:

//Not sure if at the end it should be .text or .value or .value()
triggerNumber = document.findcontrol(txtTrigNo).text;
var validTriggers = [123, 456, 789];
if (validTriggers.indexOf(parseInt(triggerNumber)) == -1) {
    alert("Invalid trigger number");
} else {
    hostAddress= top.location.host.toString();        
    url = "http://" + hostAddress "/" + triggerNumber;

Finally, if 12 the destination is a server-side page (php, asp, etc), the 11 address usually looks like this:

"http://" + hostAddress "/trigger.php?id=" + triggerNumber;

but you'd 10 better use forms for this.

Edit: As Cerbrus suggested, validating 9 the values with javascript is a good way 8 to tell the user about his errors before 7 navigating away from the page. But to make 6 sure the correct data is sent to server, it 5 is important to do the validation in the 4 server-side code, too.

In this example, in 3 case of an invalid trigger number the user 2 may finally see a 404 error; but with sensitive 1 information worse things can happen.

Score: 5

What you need is:

document.location.href = url;

After you have the URL 2 in the url variable.

To get value of input element 1 have:

var triggerNumber = document.getElementById("txtTrigNo").value;
Score: 0

This will get the hostname and port of the 3 server, and concatenate the value of the 2 element onto the end, and then go to the 1 resulting URL.

var triggerNumber = document.getElementById("txtTrigNo").value();
var url = "http://"+window.location.host+"/"+triggerNumber;
window.location = url;

More Related questions