Jump to content
freddybrown

Can't Open URL in New Tab

Recommended Posts

I am new to website development. I can't figure out how to open a new tab for the following javasript. I went through related questions but nothing worked for me. I want the browser to open a new tab when someone clicks on the link (example.com) below. The code target="_blank doesn't work here... What code should I add and where should I place that code?

 
My html code is here -
<section id="work-grid" class="site-inner">
<div class="work-item" data-groups='["all", "webdesign"]' data-url="http://example.com">
<figure class="image-container"><img src="images/work/web-one.jpg" /></figure>
</div>
Thanks in advance.

Share this post


Link to post
Share on other sites

This jQuery will probably work, but only on browsers that don't block popups. There is no guarantee any link with _blank will open a new tab. It depends on the individual users browser settings.

$('div.work-item').on('click', function() {
  window.open($(this).data('url'), '_blank');
});

Share this post


Link to post
Share on other sites

Also won't work if user has javascript disabled. If you want foolproof, wrap the image in an anchor, which has a native click event. div's don't.

Share this post


Link to post
Share on other sites

i agree with cronix. mostly. rather than wrapping the img in an anchor tag, might work better to just style the anchor with background image. would replace the div with an <a></a>

something like:

<a href="http://example.com" id="example_link"></a>


css:

#example_link {
    display:block;
    width:[width of image];
    height:[height of image];
    background:url('images/work/web-one.jpg');
}

Share this post


Link to post
Share on other sites

Personally I think that using css is probably going to be your best bet in this situation.. until I can see your java script at least.

#69{
    display:block;
    width:69;
    height:69;
    background:url('#');
}

You can then link simply by doing something as easy as.

<a href="#" id="69"></a>

If you're unaware of what this is doing it's basically doing the same thing that including a straight image would do.

<a href="#"><img src="#"/></a>

Going to keep an eye on this thread in hopes of the Java being posted because this method will get annoying very quickly.

Share this post


Link to post
Share on other sites

@shiftoii They were discussing javascript, not JAVA, which is a totally different language.

Share this post


Link to post
Share on other sites

You could try this 

<div class="work-item" data-groups=/["all", "webdesign"]' onClick="example()">

<script>
function example() {
window.open("http://example.com", "_blank");
}
</script>
Edited by Tom8001

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.