How to open a new window on click of a link using JavaScript
In Category HTML JavaScript
In JavaScript, window.open method can be used to open link in a new window. This method can used to open a new window by setting JavaScript as target for a link.
You might be wondering why do we need to use JavaScript for a link as browser can open the link automatically. This method is useful if you want to hide the target URL of a link. Typically power users look at the status bar of the browser before they click on a link. This way you can hide the URL from display in status bar and encourage the user to proceed. This method also helps hide URLs from automatic web site downloader tools like “wget”.
The following example shows how a new window can be opened with a URL.
<script type="text/javascript"><!--
function openLink() {
window.open('http://techpulp.com');
}
//--></script>
Place the following in your page wherever you would like the link to appear.
<a href="javascript:openLink()">My Link</a>
The “Hidden Link” in the following links is created using the above example and “Standard Link” is a standard way of creating a link in HTML. Just move the mouse over the following links and see what appears in the status bar of the browser. You will see “javascript:openLink()” for the hidden link while the standard link shows the target URL in the status bar.
Recent Comments