Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7743029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:25:42+00:00 2026-06-01T09:25:42+00:00

I’m trying to hijack a button element so I can open it’s link in

  • 0

I’m trying to hijack a button element so I can open it’s link in a new window. I’ve added a new attribute the elements which need to open in a new window called data-ext-link="true". In some cases the button element has a onclick which contains the location.href that I need to send the user to. How can I grab that location.href using jquery so I can supply it to the window.open in my function?

The function below works great for anchor elements, but it’s within my is('button') if statement where it gets hairy and doesn’t work because attr() returns an object and not a string. The object It returns is the following:

My Button Code:

<button data-ext-link="true" onclick="location.href='http://website/url_i_want'" class="btn2">Open it up</button>

$.attr(‘onclick’) Response:

function onclick(event) {
    location.href = "http://website.com/url_i_want";
}

My Function:

function extLinks() {
    var extLinks = $('a[data-ext-link="true"], button[data-ext-link="true"]');

    $.each(extLinks, function(){
        if($(this).is('button')){
            var link = $(this).attr('onclick'); // Here it needs to be a string.
            link = link.match(/location.href='(.*)'/); //Won't work because it's an object.
            link = link[1];
        } else {
            var link = $(this).attr('href');
        }
        $(this).unbind('click.extLink').bind('click.extLink',function(e){
            e.preventDefault();
            window.open(link);
        });
    });
}

UPDATE:

I found the following related question which suggest using $.live which being deprecated I’d use $.delagate() instead, but this doesn’t fully address the issue where I need to tell this button to open in a new window using it’s location.href value inside the onclick as the target.

SOLUTION:

function extLinks() {
    var extLinks = $('a[data-ext-link="true"], button[data-ext-link="true"]');

    $.each(extLinks, function(){
        if($(this).is('button')){
            var link = this.onclick.toString().match(/location\.href\s*=\s*['"]([^'"]*)['"]/)[1];
        } else {
            var link = $(this).attr('href');
        }
        $(this).unbind('click.extLink').bind('click.extLink',function(e){
            e.preventDefault();
            window.open(link);
        });
    });
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T09:25:43+00:00Added an answer on June 1, 2026 at 9:25 am

    Try to convert the function to a string:

    var link = $(this).attr('onclick').toString();
    

    Note – I don’t know if this will work in all browsers. Works for me in Firefox and IE7 though.


    Also, you may need to revise your regular expression – it won’t work for the example you posted. Try this:

    /location\.href\s*=\s*['"]([^'"]*)['"]/
    

    I’ve confirmed that this solution does work for me. Here’s an example page: http://nylen.tv/test-onclick.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.