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.
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 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');
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 just need to get the view code from youtube urls. The api is returning back strings that look like this:
http:\/\/www.youtube.com\/watch?v=XODUrTtvZks&feature=youtube_gdata_player
I need to get this part:
XODUrTtvZks
from the above, keep in mind that sometimes there may be additional parameters after the v=something like:
&feature=youtube_gdata_player
and sometimes there may not be. Can someone please provide the regex that would work in this situation and an example of how to use it using javascript?
You can use /v=([^&]+)/ and get the match at offset 1.
This snippet only matches on URL's from youtube.com:
var url = 'http://www.youtube.com/watch?v=XODUrTtvZks&feature=youtube_gdata_player';
var matches = url.match(/^http[s]?:\/\/www.youtube.com\/watch\?\s*v=([^&]+)/i);
if (matches) {
var videoID = matches[1];
// do stuff
}
You can use an online tool called RegExr to get your regular expression ,[http://gskinner.com/RegExr/].
Regards
Rahul
This snippet is from Google’s own parser at closure:
function getIdFromUrl(url) {
return /https?:\/\/(?:[a-zA_Z]{2,3}.)?(?:youtube\.com\/watch\?)((?:[\w\d\-\_\=]+&(?:amp;)?)*v(?:<[A-Z]+>)?=([0-9a-zA-Z\-\_]+))/i.exec(url)[2];
}
You can see it here:
http://code.google.com/p/closure-library/source/browse/trunk/closure/goog/ui/media/youtube.js?r=1221#246
what I would like to do is to the html page for a specific string and read in a certain amount of characters after it and present those characters in an anchor tag.
the problem I'm having is figuring out how to search the page for a string everything I've found relates to by tag or id. Also hoping to make it a greasemonkey script for my personal use.
function createlinks(srchstart,srchend){
var page = document.getElementsByTagName('html')[0].innerHTML;
page = page.substring(srchstart,srchend);
if (page.search("file','http:") != -1)
{
var begin = page.search("file','http:") + 7;
var end = begin + 79;
var link = page.substring(begin,end);
document.body.innerHTML += 'LINK | ';
createlinks(end+1,page.length);
}
};
what I came up with unfortunately after finding the links it loops over the document again
Assisted Direction
Lookup JavaScript Regex.
Apply your regex to the page's HTML (see below).
Different regex functions do different things. You could search the document for the string, as suggested, but you'd have to do it recursively, since the string you're searching for may be listed in multiple places.
To Get the Text in the Page
JavaScript: document.getElementsByTagName('html')[0].innerHTML
jQuery: $('html').html()
Note:
IE may require the element to be capitalized (eg 'HTML') - I forget
Also, the document may have newline characters \n that might want to take out, since one could be between the string you're looking for.
Okay, so in javascript you've got the whole document in the DOM tree. You an search for your string by recursively searching the DOM for the string you want. This is striaghtforward; I'll put in pseudocode because you want to think about what libraries (if any) you're using.
function search(node, string):
if node.innerHTML contains string
-- then you found it
else
for each child node child of node
search(child,string)
rof
fi