jQuery Load New Window

Sam Deering

jQuery code snippet to load/open a link in new window. This code adds an event to the anchor tags that are given the “new-window” class and forces them to open in a new window.

$(function(){
 $('a.new-window').click(function(){
 window.open(this.href);
 return false;
 });
});

Advanced jQuery Load New Window Example

This code gets the id of a container div and then grabs the hidden url div element and then opens it in a new window.

function openblog(blog_id) {
	//alert(blog_id);
	$('#blog-wrap-'+blog_id).hide();
	var blogurl = $('#'+blog_id+'-url').text();
	var location = "http://domainname/index.php?blogurl="+blogurl;
	window.open(location);
}

The HTML Code

<div class="blog-wrap" id="blog-wrap-blog_id">
<div id="blog-id-url" style="visibility:hidden" class="hidden">http://blogoola.com</div>
<p><a href="javascript:openblog(blog_id)>Open Blog</a></p>
</div>

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

No Reader comments

Comments on this post are closed.