2

With the below script I was trying to load the output of the variable "url" in the browser on clicking the anchor tag, while executing it shows the expected result path in the alert box but it loads the path mentioned in the anchor tag. I want to load the output path in the browser's same tab. please help me to solve this.

<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script>
    $(document).ready(function(){   
        $("a").on('click', function(){
            var reqURL = window.location.href;
            var hrefVal = $(this).attr('href');
            var url;        
            if(hrefVal.indexOf("http")== -1){
                if(reqURL.indexOf("/ca-en") != -1){
                    url="http://www.mywebsite.com/site2"+hrefVal;
                }else if(reqURL.indexOf("/ca-fr")> -1){
                    url="http://www.mywebsite.com/site3"+hrefVal;
                }else{
                    url="http://www.mywebsite.com"+hrefVal;     
                }
                alert(url);
                window.open(url,"_self");
            }       
        });
    });
    </script>
</head>

<body>
    <ul>
    <li>
        <a class="category" href="/home.html">home<span class="icon plus"></span></a>
        <ul class="submenu">
            <li><a href="/home/abc.html">abc</a></li>
            <li><a href="/home/xyz.html">xyz</a></li>
            <li><a href="/home/tam.html">tam</a></li>
        </ul>
    </li>
    </ul>
</body>
</html>
thamizhinian
  • 83
  • 1
  • 11

4 Answers4

0

You need to add onclick function on anchor tag. otherwise it will always redirect to href path of anchor tag before the javascrip window.open load.

<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script>

    function redirect(parameter){
            var reqURL = window.location.href;
            var hrefVal = parameter;
            var url;        
            if(hrefVal.indexOf("http")== -1){
                if(reqURL.indexOf("/ca-en") != -1){
                    url="http://www.mywebsite.com/site2"+hrefVal;
                }else if(reqURL.indexOf("/ca-fr")> -1){
                    url="http://www.mywebsite.com/site3"+hrefVal;
                }else{
                    url="http://www.mywebsite.com"+hrefVal;     
                }
                alert(url);
                window.open(url,"_self");

            }
    }

    </script>
</head>

<body>
    <ul>
    <li>
        <a class="category" href="/home.html">home<span class="icon plus"></span></a>
        <ul class="submenu">
            <li><a href="#" onclick="redirect('/home/abc.html')">abc</a></li>
            <li><a href="#" onclick="redirect('/home/xyz.html')">xyz</a></li>
            <li><a href="#" onclick="redirect('/home/tam.html')">tam</a></li>
        </ul>
    </li>
    </ul>
</body>
</html>
Vatsal Shah
  • 1,334
  • 18
  • 21
0
- <li><a href="/home/abc.html">abc</a></li>

+ <li><a href="javascript:void(0);" data-url="/home/abc.html">abc</a></li>


- var hrefVal = $(this).attr('href');

+ var hrefVal = $(this).data('url');

Very simple..

0

The issue is about when you are using <a> tag, it used to redirect to whatever you value you given in href. So before executing your script you are suppose to prevent default behavior of you <a> tag.

Use event.preventDefault() in your onclick script. This prevent default redirection of your <a> tag.

Find the fiddler for your reference.

Hope it helps.

RaJesh RiJo
  • 4,302
  • 4
  • 25
  • 46
0

The default behaviour of the anchor tag has to be stopped prior to executing your custom behaviour logic. your code can be written as follows:

<script>
$(function(){   
    $("a").on('click', function(e){
        e.preventDefault();
        var reqURL = window.location.href;
        var hrefVal = $(this).attr('href');
        var url;        
        if(hrefVal.indexOf("http")== -1){
            if(reqURL.indexOf("/ca-en") != -1){
                url="http://www.mywebsite.com/site2"+hrefVal;
            }else if(reqURL.indexOf("/ca-fr")> -1){
                url="http://www.mywebsite.com/site3"+hrefVal;
            }else{
                url="http://www.mywebsite.com"+hrefVal;     
            }
            alert(url);
            window.open(url,"_self");
        }       
    });
});
</script>
Parthipan Natkunam
  • 756
  • 1
  • 11
  • 16