Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call a url in javascript on click

I have a javascript function. On-click will call this. Using document.getElementById I am getting certain parameters there. Using that parameters I need to build a url. ie, onclick will have to execute that url.

For example, in javascript,

function remove()
{
var name=...;
var age = ...;
// url should be like http://something.jsp?name=name&age=age
}

In short I need to execute http://something.jsp?name=name&age=age this url on click

<input type="button" name="button" onclick="remove();" />
like image 884
Nidheesh Avatar asked Jul 20 '12 10:07

Nidheesh


People also ask

How do I go to a specific URL in JavaScript?

JavaScript window. The syntax for this approach is: window. location = "url"; “url” represents the URL you want the user to visit.

How do I programmatically click a link with JavaScript?

The JavaScript to click it: document. getElementById("my-link-element"). click();

Can you use onclick on a link?

This is a type of JavaScript link - the onclick attribute defines a JavaScript action when the 'onclick' event for the link is triggered (i.e. when a user clicks the link) - and there is a URL present itself in the onclick attribute.


3 Answers

Use window.location:

function remove()
{
    var name = ...;
    var age  = ...;

    window.location = 'http://something.jsp?name=' + name + '&age=' + age;
}
like image 79
Wouter J Avatar answered Sep 21 '22 14:09

Wouter J


I use:

document.location.href = "report.html";

So, in your case:

function remove() {
    var name=... ,
        age = ...;

    document.location.href = "something.jsp?name=" + name + "&age=" + age;
}
like image 21
Nick Avatar answered Sep 18 '22 14:09

Nick


just call

window.location=myURL;

in your function

like image 23
kommradHomer Avatar answered Sep 20 '22 14:09

kommradHomer