LearnFast.Ninja

JavaScript href onclick example redirect like a href

Window open JavaScript to open new window example. We create div element, then add JavaScript to the onclick event. We add CSS style for pointer cursor so the user will know that it is a clickable. Try the working example code and see how easy is to change div into link. If you want JavaScript to open url in same window you need to add the string parameter _self.

Example 1: Open link in new window.
window.open('http://www.thebestdata.com/');

Example 2: Open link in same window.
window.open('http://www.thebestdata.com/''_self');

Working example code: 
We create div element, then add JavaScript to the onclick event. 
We add CSS style for pointer cursor so the user will know that it is a clickable.

<!DOCTYPE html>

<html>

<head>

    <title>Example</title>

</head>

<body>

    <div onclick="window.open('http://www.thebestdata.com/');" style="cursor: pointer;">

        This is link div...      

    </div>

</body>

</html>


Tags

javascript

href link javascript-redirect blank