I have seen several questions about redirecting to a page via javascript, and that's great, but all of them just redirect to a base url. I want to include a variable path. So say for instance I have http://www.glas.com/my/url/path I want to use window.location.replace() to replace the glas part only, and change it to glass. I'm a beginner at JavaScript so any help would be much appreciated.
This is what I have tried so far:
var path = ''; // this is where I'm stuck
window.location.replace("http://www.glass.com/" + path);
Where the path variable would parse the current URL for anything after the /. I have tried looking at JavaScript's match and regexp, but it all seems very confusing.
Try
window.location = window.location.href.replace('glas', 'glass');
You would do something like:
window.location.href = window.location.href.replace('glas','glass');
Related
I am trying to figure out on how do I strip a part of URL and set the rest as source using JQuery? (The part of the URL we're trying to get rid of is the actual domain we are on). SO this will be like forcing the internal link to external one by stripping the main domain name and keeping the rest.
Original Code:
<li data-src="http://khan.zaha.in/https://storage.googleapis.com/shahbano-khan/Media/Audio/Desmeon%20-%20Back%20From%20The%20Dead.mp3" data-title="1. Back From the Dead" class="song-row">
What i need is:
<li data-src="https://storage.googleapis.com/shahbano-khan/Media/Audio/Desmeon%20-%20Back%20From%20The%20Dead.mp3" data-title="1. Back From the Dead" class="song-row">
So we would be getting rid of the originating domain name.
If there is other ways to achieve this, I will be glad to try.
Looking forward to gain healthy help.
Regards,
Syed H
We can strip off unwanted string using replace and set it back using attr(attributeName, value) methode
var originalStr = $('li').attr('data-src'); // get current src
var strTobeRemvd = 'http://khan.zaha.in/';
var modified = originalStr.replace(strTobeRemvd,''); //remove the unwanted str
$('li').attr('data-src', modified);// set the modified src back
console.log(modified)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li data-src="http://khan.zaha.in/https://storage.googleapis.com/shahbano-khan/Media/Audio/Desmeon%20-%20Back%20From%20The%20Dead.mp3" data-title="1. Back From the Dead" class="song-row">
If you want to remove all such occurances from any Url and not just from khan.zania domain, this will prove to be more useful as it basically takes the portion of after first http:// or https:// and find the first / till end of url.
$('li[data-src^="http"]').each(function(){
var data=$(this).attr('data-src');
$(this).attr('data-src',data.slice(data.indexOf('/',8)+1));
});
Tried and tested. This works. Hope it helps.
You're best doing this via regex, then its not domain specific:
_src = $('.song-row').data('src');
_src = _src.replace(/http:\/\/(?:www\.)?([a-z0-9\-]+)(?:\.[a-z\.]+[\/]?)/g,'');
$('.song-row').attr('data-src', _src);
You can see it working here - https://jsfiddle.net/f6f7wwto/2
I have javascript code which finds and replaces all instances of a value on a page. I am working with a CMS so unfortunately I need to work around lack of flexibility with the base HTML.
document.body.innerHTML = document.body.innerHTML.replace(/VALUE/g, new_value);
I want to find a replace a url link using the same process. How do I take into account for the"/" in the search and replace strings?
Example:
find_str = "www.mysite.com/folder";
replace_str = "app.mysite.com/page";
document.body.innerHTML = document.body.innerHTML.replace(/find_str/g, replace_str);
Thank you both for your replies. I have taken advice from both and changed my code as follows:
jQuery('a[href="tttps://www.mysite.com/folder"]').attr("href", "tttps://app.mysite.com/page");
PS: I cannot add https:// to the above as it will not let me save, so I added tttps:// instead.
I want to return a value in a function depending on the Location URL from within an iFrame.
The value is 1 if the iFrame is on the main site page, and 2 if anywhere else on the site. Currently it only returns 1.
I'm new to RegEx and have a feeling it is my syntax.
Any help would be hugely appreciated!
Here is what I have so far:
var LocationURL = document.referrer;
re = new RegExp("/^(http:\/\/)?(www.)?(website.co.uk/)[a-zA-Z0-9/.]+$/");
bFound = re.test(LocationURL);
function getJoinPageLoc()
{
return (bFound == true)? 2 : 1;
}
console.log(getJoinPageLoc());
I am trying to allow it to start with http://www. or just www. or nothing.
I am assuming that any other location on the site will start with / so I am using website.co.uk/ as an absoloute (obviously website will be the actual website), followed by anything else (to make sure it's not the main page).
I hope this makes some sense!
Edit:
Still trying. Have tried this also but still not working:
re = new RegExp("/^(https?:\/\/)?(www\.)?website\.co\.uk/([a-zA-Z0-_9/\.]+)&/");
The problem was with document.referrer picking up the wrong frame >_<
Have had to, insteadm pass in a parameter through the top location and work this the other way round. Which in retrospect is much simpler >_<
My website URL is (example) http://mypage.com/en/?site=main. Then comes JavaScript code that, together with PHP, parses the data.
After that, I need some code that will change the URL inside the adress bar to http://mypage.com/en/, that is, removes the stuff after the last / (slash).
If possible, it is should be jQuery/JavaScript code.
I found something that will work.
You have to use a method called replaceState().
Mozilla developer reference
var str = window.location.href;
window.history.replaceState({}, '', str.substr(0,str.lastIndexOf("/"))+"/");
Use split() function in javascript.
Example :
var url = "http://mypage.com/en/?site=main";
alert(url.split('?')[0]);
I'm trying to set a cookie with a value that is made up of part of a referring url. The referring url looks something like:
http://webct.university.ac.uk/webct/urw/lc715920578061.tp721521425061/courseMenu.dowebct?
and I need to extract:
lc715920578061.tp721521425061
for reuse in another function.
Would this be regex? Could anyone help with the syntax?
Many thanks
Matthew
You can use replace with a regex and split like this:
var desired_part = document.referrer.replace(/^https?:\/\//gi, '').split('/')[3];