javascript - Jquery issue on changing the url or loading the url -
with below script trying load output of variable "url" in browser on clicking anchor tag, while executing shows expected result path in alert box loads path mentioned in anchor tag. want load output path in browser's same tab. please me 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>
the default behaviour of anchor tag has stopped prior executing custom behaviour logic. code can written 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>
Comments
Post a Comment