6

What am I doing wrong?

  1. Only on pages with the URL location of "/MyWebsite/Example.aspx" append "?template=PW"
  2. But only to links that contain "/HelloWorld/default.aspx"

There is no ID or classes associated to this link, so I have to look for the URL.

This is my code, but the links are not updating.. I know I'm close!

$(document).ready(function(){
 if (document.location.href.indexOf('/MyWebsite/Example.aspx') > 0) 
 {
 $('a[href*="/HelloWorld/default.aspx"]').append("href",$("?template=PW"))
 }
});
asked May 6, 2013 at 23:04

5 Answers 5

5
$(document).ready(function(){
 if (document.location.href.indexOf('/MyWebsite/Example.aspx') > 0) 
 {
 var $el = $('a[href*="/HelloWorld/default.aspx"]');
 $el.attr("href", $el.attr("href")+ "?template=PW");
 }
});
answered May 6, 2013 at 23:09

3 Comments

I'd like to accomplish the same things but with all href how can I do that? please mention me @Shaz3e on twitter with your reply.
@shaz3e, use $.each method to loop all anchors, $("a").each(function() { $(this).attr("href", $(this).attr("href")+ "?template=PW"); });
I have already achieved this by using the following code. <script type="text/javascript"> var querystring = 'MyString'; // Replace this $('a').each(function(){ var href = $(this).attr('href'); href += (href.match(/\?/) ? '&' : '#') + querystring; $(this).attr('href', href); }); </script>
4

This would definitely solve your problem as it did mine.

<script type="text/javascript">
 var querystring = 'MyString'; // Replace this
 $('a').each(function(){
 var href = $(this).attr('href');
 href += (href.match(/\?/) ? '&' : '?') + querystring;
 $(this).attr('href', href);
 });
</script>
BuddhiP
6,4835 gold badges38 silver badges56 bronze badges
answered Mar 30, 2017 at 13:27

Comments

3

Use $.attr() to edit an attribute.

$.append() is used to insert an html child node inside your element.

$(document).ready(function(){
 if (document.location.href.indexOf('/MyWebsite/Example.aspx') > 0) 
 {
 var href = '/HelloWorld/default.aspx';
 $('a[href*="' + href + '"]').attr("href", href + "?template=PW")
 }
});
answered May 6, 2013 at 23:10

Comments

1

Have you consider using PURL? With PURL I was able to do this:

var url = "http://localhost/some/url?item1=one&item2=two";
if ($.url(url).attr('query')) {
 url = url + '&newItem=new';
}
else {
 url = url + '?newItem=new';
}
answered Mar 11, 2014 at 8:42

Comments

1

Replace

$('a[href*="/HelloWorld/default.aspx"]').append("href",$("?template=PW"))

With

$.each(
 $('a[href*="/HelloWorld/default.aspx"]'),
 function(index, value) {
 $(value).attr('href', $(value).attr('href') + '?template=PW');
 }
);

That will get you started, but you should also check to make sure there isn't a query string parameter already in the matched URL.

$().append() doesn't alter the value of the href attribute, it is used to insert content at the end of each matched element. The jQuery documentation has examples of how to use $().append().

Paolo
21.3k21 gold badges78 silver badges124 bronze badges
answered May 6, 2013 at 23:18

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.