Link to home
Start Free TrialLog in
Avatar of yingwho
yingwho

asked on

adding substring

i have some urls, i want to change the path on click by adding a substring (/myfolder/) in the path.
for eg,  http://www.time.com/time/magazine  should become  http://www.time.com/myfolder/time/magazine
pl suggest.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>  
<script>
$(document).ready(function() {
	$(function() {	
			$("a").click(function() {
				if (($(this).attr("href").indexOf("time.com") >= 0)) {				
				var t =  $(this).attr("href");
				alert(t);
				var t1 = ($(this).attr("href")).substr(0, 18);
				alert (t1);
				var lin ='/myfolder/'
				($(this).attr("href")) =   t1+ lin + t; 
				return false;
				}
			});
		}
	});
});
</script>
</head>
<body>
<a href="http://www.time.com/time/magazine">mag</a> <br />
<a href="http://www.time.com/time/people">people</a> <br />
<a href="http://www.time.com/time/nation">nation</a>
</body>
</html>

Open in new window

Avatar of hielo
hielo
Flag of Wallis and Futuna image

you should be doing this at load time, not upon click. Otherwise on every click of the same link you would be trying to add /myfolder over and over. try:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>  
<script>
$(document).ready(function() { 
	   	$('a').each(function(){
			this.href = this.href.replace(/time\.com/,'time.com/myfolder')
			
		});
});
</script>
</head>
<body>
<a href="http://www.time.com/time/magazine">mag</a> <br />
<a href="http://www.time.com/time/people">people</a> <br />
<a href="http://www.time.com/time/nation">nation</a>
</body>
</html>

Open in new window

Avatar of yingwho
yingwho

ASKER

heilo

thanks.
1 problem
its doing  time 2 times.. so let me change my question as below..

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>  
<script>
$(document).ready(function() {
                $('a').each(function(){
                        this.href = this.href.replace(/time\.com/,'time.com/myfolder')
                       
                });
});
</script>
</head>
<body>
<a href="http://www.time.com/magazine">mag</a> <br />
<a href="http://www.time.com/people">people</a> <br />
<a href="http://www.time.com/nation">nation</a>
</body>
</html>

please make  http://www.time.com/magazine to  http://www.time.com/myfolder/magazine etc
ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of yingwho

ASKER

great. thanks
Avatar of yingwho

ASKER

hielo.. u have written lot of tricky scripts and impressed me..
if u can solve my trickiest problem below i will be flat!
this Q is around for a while with 10 answers already, but still not usable.

https://www.experts-exchange.com/questions/26117631/popup-communication.html