Change host of all links on page with js/jquery - javascript

There is a website example.com. I want to change all links on that website so they appear to different host. I need to change these types of links:
Link
Link
Link
to
Link
Link
Link
While leaving form and js urls as is. I wonder what is the less painful way to do that? I know that parsing urls with regex is bad, so how can I parse these urls as a class and do operations to them?

you could try this:
$('a').each(function(){
var $me = $(this);
var url = $me.attr('href');
var newUrl = url.replace('example','mirror');
if(newUrl.indexOf('mirror') < 0) newUrl = 'mirror.com/' + url;
$me.attr('href',newUrl);
})
this will change all the <a> and only them (no forms and script href)

Related

URL Paramters carried between pages - how to stop tel: also?

I have this really basic script that changes all the links on my wordpress site to preserve the url param - I use it on wordpress landing pages for my business
var queryString = new URL(window.location).search;
document.querySelectorAll("[href]").forEach(link => {
var current = link.href;
link.href = current + queryString;
});
E.g.
website.com/?location=Australia
All links on the page will carry over the parameter to whatever href link my visitor clicks
/book-online/?location=Australia
/request-a-callback/?location=Australia
However this is obviously changing all my href links, and is changing my tel: links with it.
Is there a way I can make it only change https://?
My tel: links look like this
tel:0293 12329?location=Australia
I have had this code copied and pasted for ages, I know absolutely nothing about javascript and no idea where I found the above code, I've just used it for ages and it works as a quick and easy way for me to track a users progress through my landing pages.
Perhaps you could only do this if tel: is not in the href?
var queryString = new URL(window.location).search;
document.querySelectorAll("[href]").forEach(link => {
var current = link.href;
if (!current.includes("tel:")) {
link.href = current + queryString;
}
});

Javascript menuext window.external.menuArguments.event.srcElement.getElementsByClassName

I'm trying to get a video url from the site, https://yandex.com/video/ via IE's menuext function that runs javascript.
So from the left in the page, how can i get the url of right-clicked element (video) via something like window.external.menuArguments.event.srcElement.getElementsByClassName
I think it's simple but I could not.
var link = window.external.menuArguments.event.srcElement.outerHTML;
if (link.indexOf('href":"') > -1){
link = link.substring(link.indexOf('href":"') +7);
link = link.substring(0, link.indexOf('"'));}

Change right click "Copy link" value

I am currently working on an affiliate website. I have made a script in jQuery that shows the sales page (without affiliate link) when hovering, but when you click on it, it adds my affiliate link to the beginning of the link. Then my network redirects them to the desired page.
EDIT: To clarify, I want to say that I am talking about hovering over links
Now, if you right click on it, a menu opens, and you can click "Copy link". This copies the "xxx" in:
Example
Which is without my affiliate link. How do I make the "Copy link" button copy another link than the href in the a tag? Is it even possible?
Right now I avoid it completely by having this in the beginning of my code:
.on("click contextmenu", function(){};
To give you an example of the current code, it works by listening to clicks on a tags with a specific class, then it checks if the href in the a tag contains some text, if it contains that text, it does this:
window.location.href = affiliatelink + link;
If it doesn't, it does this (if I don't have an affiliate link for that product):
window.location.href = link;
So, is my problem possible to solve - or do I have to do it, like I am doing it?
EDIT 2: I have to provide my code
$(document).ready(function(){
$(".class").on("click contextmenu", function(){
var link = this.href;
var partner = "https://affiliatelink.com/?url=";
if(link.indexOf("partnername") != -1){
window.location.href = partner + link;
} else {
window.location.href = link;
}
});});
Assuming I'm understanding your question correctly, it might be possible to solve if you change when you're appending your affiliate code to links.
The "copy link address" context menu shortcut is going to copy whatever is in the href attribute of a link tag. So, what if you set that attribute's value to your affiliate link + the original link's text? You would no longer need to do it in a click handler; it would "just work" and would support both cases.
Something like this might do the trick:
var links = [].slice.call(document.querySelector('.your-class'));
links.forEach(function(link){
link.href = affiliateLink + link.href;
});
You'd no longer need your click handler, and it supports both cases of interacting with links.
EDIT: Updated with your code sample.
$(document).ready(function(){
$(".class").each(function(){
var link = this.href;
var partner = "https://affiliatelink.com/?url=";
if(link.indexOf("partnername") != -1){
this.href = partner + link;
}
});});

Append main url with javascript withour refreshing the page doesnt work

I have list of videos on a page and i show play each video on the same page when one clicks on the video icon, My sample page url is like
www.xyz.com/video/
Now i have to append the url in such a way that when a user click on the video icon it should also play that perticular video on the same page and also change the url to
www.xyz.com/video/XhskwaJS without refresh the page. Logic behind this is to share give user ability to share each video rather than the main video page which has several videos listed
I used following code on event click to extract the VideoID and try to append it to the main URL
var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
var URL = 'http://www.youtube.com/embed/Se1y2R5QRKU?showinfo=0&modestbranding=1'
var match = URL.match(regExp);
if (match && match[7].length == 11) {
//console.log(match[7]);
location.href = $(this).attr('href') + match[7];
return match[7];
} else {
//alert("Could not extract video ID.");
}
Your regex won't match when applied to a youtube.com address as it requires youtu.be.
Maybe you can simplify the regex anyway by specifying one alternative for embed and watch urls along the lines of
^[^?]+/embed/([^?]+)\?
^[^?]+/watch\?v=([^&]+)
Finally, instead of setting location.href which triggers a refresh, use the html5 history api and write
window.history.replaceState('Object', 'Title', $(this).attr('href') + match[7]);

Javascript/Jquery window.location

I have page the has some data in tabs, Im trying to write a function so that when links are click from another page can load the page with the tabs on and show the correct tab. This is working with the code below, minus the actual changing tabs function. But for some reason using the window.location..... as a variable still scroll the page down to the matching id. Is there another way to get the string in the url after the #. Or can i do it this way but not have to jump to the id? thanks
function loadTab(){
var linkToTab = window.location.hash.substr(1);
var linkClass = '.'+linkToTab
if(window.location.hash != '') {
changeTabs(linkClass);
}else{
$('.companyLink:first').addClass('active');
$('.companyBio:first').addClass('active');
$('.companyBio:first').fadeIn();
};
}
The hash character is for anchors.
Use a question mark instead of a hash.
<a href="index.html?tabname">
and
var linkToTab = window.location.search.substr(1);
var linkClass = '.'+linkToTab
if(window.location.search != '') {
The # part of the URL is traditionally used for anchors. The 'jumping' you see is the original feature.
Modern websites and web-applications use it build a history as HTML5's history feature isn't widely supported yet.
To avoid the jumping add event.preventDefault to your links, like:
Tab1
<script type="text/javascript">
document.getElementById("tab1handle").onclick = function (event) {
event.preventDefault();
}
</script>
You can also make sure the anchor is not defined. In this case the browser will jump to the top of the page. It's up to you whether this is undesirable or not.

Categories