Search code examples
javascriptjqueryhtmlgreasemonkey

javascript get href address and change


i need to get href value and change it and open in new tab Automatically.

Address tag in the page.

<a href="http://xxxxx/first_123.html" id="view-1" class="view button_green" style="margin: 10px;">view</a>

I want to get http://xxxxx/first_123.html address with JavaScript and Change to

http://xxx/second_123.html

And then Open http://xxx/first_123.html In new tab Automatically.

I Use Greasemonkey. Thanks.


Solution

  • HTML

    <a href="http://xxxxx/first_123.html" id="view-1" class="view button_green" style="margin: 10px;">view</a>
    

    JavaScript

    $(document).ready(function(){
          $("#view-1").click(function(e){
            e.preventDefault();
            var _curr_href= $(this).attr("href");
            $(this).attr("href", _curr_href.replace("first", "second"));
            window.open(_curr_href);
          });
    });