Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I go to a new page from within javascript?

Tags:

javascript

I have a link like this:

<a href="www.example.com">

I would like to simulate the clicking of this link within javascript with a function that is called with the left arrow but I am not sure how to do it.

Now I have this:

$(window).keypress(function (e)
{
   if (e.keyCode == 37)
   {
    //  position 1
   }

});

But I am not sure how to change the page to the link "www.example.com" from within javascript at position 1. Can someone help me with this.

like image 833
Alexandre Avatar asked Jun 12 '11 08:06

Alexandre


People also ask

How do you go to a new page in JavaScript?

JavaScript window. To navigate to a new URL, use the location object from the Browser's History API. The session history lets you reassign the location object to a new URL or use the href property on that same object. The syntax for this approach is: window.

How do I redirect to another page?

Approach: To redirect from an HTML page to another page, you can use the <meta> tag by specifying the particular link in the URL attribute. It is the client-side redirection, the browsers request the server to provide another page.

How do I navigate to another page in HTML?

How to Redirect to Another Page in HTML. To redirect one HTML page to another page, you need to add a <meta> tag inside the <head> section of the old HTML page. The <head> section of an HTML document contains metadata that is useful for the browser, but invisible to users viewing the page.


2 Answers

You could use the following to perform a redirect:

window.location.href = 'www.example.com';
like image 77
Darin Dimitrov Avatar answered Nov 13 '22 08:11

Darin Dimitrov


You could set window.location to the url you want the page to load, like:

window.location = "www.example.com"
like image 22
Michael Robinson Avatar answered Nov 13 '22 10:11

Michael Robinson