toolman Posted July 28, 2016 Share Posted July 28, 2016 Hi there, I have the following URL: http://www.website.com/?code=code1&ref=myref&utm_source=email&utm_medium=direct&utm_campaign=test What I would like to do is add the ?code=code1&ref=myref&utm_source=email&utm_medium=direct&utm_campaign=test part of the URL to specific links on the page based on their ID's. The ?code= and ?ref= are dynamic, so I would need to get the URL as a dynamic URL. What would be the best way to do this? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/301671-get-part-of-url-and-add-to-specific-links/ Share on other sites More sharing options...
requinix Posted July 28, 2016 Share Posted July 28, 2016 Use parse_url with PHP_URL_QUERY to grab just that part. Quote Link to comment https://forums.phpfreaks.com/topic/301671-get-part-of-url-and-add-to-specific-links/#findComment-1535213 Share on other sites More sharing options...
toolman Posted July 28, 2016 Author Share Posted July 28, 2016 (edited) Hi, I don't have PHP, I only want to work with jQuery :/ I've managed to create this: var urls = window.location.href; $("a").each(function() { var $this = $(this); var _href = $this.attr("href"); $this.attr("href", _href + urls.substring(urls.lastIndexOf('/') + 1)); }); This sort of works on the following code: <a href="/sub/page.shtml" onClick="window.open('/sub/page.shtml', ''); return false; trackOutboundLink(this, 'Outbound Links', 'my link');" >Link</a> However, the window.open event causes the link to lose the appended URL. How can I apply the above to append the URL in the window.open? Thanks Edited July 28, 2016 by toolman Quote Link to comment https://forums.phpfreaks.com/topic/301671-get-part-of-url-and-add-to-specific-links/#findComment-1535214 Share on other sites More sharing options...
requinix Posted July 28, 2016 Share Posted July 28, 2016 Ha, yeah, I just noticed what forum this is in... So you need the current page's query string? Try document.location.search (which includes the question mark). Quote Link to comment https://forums.phpfreaks.com/topic/301671-get-part-of-url-and-add-to-specific-links/#findComment-1535215 Share on other sites More sharing options...
toolman Posted July 28, 2016 Author Share Posted July 28, 2016 Yes that is correct Quote Link to comment https://forums.phpfreaks.com/topic/301671-get-part-of-url-and-add-to-specific-links/#findComment-1535216 Share on other sites More sharing options...
.josh Posted August 4, 2016 Share Posted August 4, 2016 (edited) Here is a basic example for what you want to do, that will work for the link example you provided: <script> $(document).ready(function(){ $('a').each(function() { var onclick = $(this).attr('onclick')||''; var qs = location.search.replace('?',''); var url=onclick.match(/window\.open\('([^']+)/); if (url&&url[0]) { newUrl = url[0] + ((url[0].indexOf('?')==-1) ? '?' : '&') + qs; $(this).attr('onclick',onclick.replace(url[0],newUrl)); } var href = $(this).attr('href')||''; if (href) { href += ((href.indexOf('?')==-1) ? '?' : '&') + qs; $(this).attr('href',href); } }); }); </script> However, this is generally bad practice, as it can easily break. The better thing to do would be to move all of the onclick code to a callback function and then attach the callback function with a click event handler. That way, you can use a variable instead of hardcoded value and then update the variable instead of trying to scrape onclick like this. sidenote: your trackOutboundLink() function will never fire because of that return false; before it. Edited August 4, 2016 by .josh Quote Link to comment https://forums.phpfreaks.com/topic/301671-get-part-of-url-and-add-to-specific-links/#findComment-1535560 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.