Jquery to get the name of the current html file - javascript

If you are on http://www.cnn.com/2012/11/09/opinion/brown-pakistan-malala/index.html can you get Jquery to grab the index.html?
or if you are on http://www.washingtonpost.com/politics/decision2012/supreme-court-to-review-key-section-of-voting-rights-act/2012/11/09/dd249cd0-216d-11e2-8448-81b1ce7d6978_story.html have it return dd249cd0-216d-11e2-8448-81b1ce7d6978_story.html?
And for non extension defined pages such as this current one http://stackoverflow.com/questions/13317276/jquery-to-get-the-name-of-the-current-html-file can it return the last "file" in the "directory"structure, for example: jquery-to-get-the-name-of-the-current-html-file

Although not JQuery, you can access it using the following:
document.location.href.match(/[^\/]+$/)[0]
or
document.location.pathname.match(/[^\/]+$/)[0] in case of unneeded anchors/hash tags (#).

location.pathname.split('/').slice(-1)[0]

No need for jQuery. This will give you the last segment of the URL path (the bit after the last slash):
var href = document.location.href;
var lastPathSegment = href.substr(href.lastIndexOf('/') + 1);

function getCurentFileName(){
var pagePathName= window.location.pathname;
return pagePathName.substring(pagePathName.lastIndexOf("/") + 1);
}

Related

How would one turn this localstorage information a clickable link?

I am simply looking to make this a hyperlink. Using Local storage I grabbed The MAKE, MODEL, and YEAR from textboxes. To then insert the values into the to the link like provided below.
https://www.jdpower.com/cars/MAKE/MODEL/YEAR
tried add to add $('result').attr('href', url); - I think I mis understood how to execute this. instead of document.getElementById('result').innerHTML = url;
.js
function generateURL(){
// document.getElementById('txtMake').value = localStorage.getItem('Make:');
// document.getElementById('txtModel').value = localStorage.getItem('Model:');
// document.getElementById('txtYear').value = localStorage.getItem('Year:');
var part1 = document.getElementById('txtMake').value;
var part2 = document.getElementById('txtModel').value;
var part3 = document.getElementById('txtYear').value;
var url = "https://www.jdpower.com/cars/"+part1+"/"+part2+"/"+part3;
document.getElementById('result').innerHTML = url;
}
.html
a tag with id="result"
any information given regarding my concern is appreciated.
Everything seems to work apart from the link being a hyperlink clickable.
If the 'id' of your link (A element) is "result", use:
$("#result").attr('href', url) ;
With the # prefix you are addressing a 'named' element. Without the # you are addressing a group of (or class of) elements.
You can simply use setAttribute method
document.getElementById("result").setAttribute("href", url);
You were correct you need to set the href attribute. The key here is $ calls a function accepting CSS selectors compared to .getElementById which just takes a name. In CSS # refers to an ID so you need:
$('#result').attr('href', url);
or without jQuery
document.getElementById('result').setAttribute('href', url);

Get a portion of string path

I have a file path as shown below.The last part (i.e. video2.mp4) is dynamically changed.I'm using Javascript/Typescript.
file:///data/user/0/com.sleep.app/files/sleep-videos/video2.mp4
Question: How to get the file:///data/user/0/com.sleep.app/files/sleep-videos/ part only from above string.
var string = 'file:///data/user/0/com.sleep.app/files/sleep-videos/video2.mp4';
var stringPart = string.substring(0,string.lastIndexOf("/")+1);
If only the filename changes try something like here
^(.+)/([^/]+)$
Try it at regexr. Your first group is the dirname, the second group the basename.
If you have further requirements/information (e.g. which tools you use), please refine your question.
var url = "file:///data/user/0/com.sleep.app/files/sleep-videos/video2.mp4";
url= url.split("/");
url.pop();
url = url.join("/");

Find Next HTML Document in a Series

I have a series of HTML files in the same directory named: page1.html, page2.html, page3.html, etc. I can identify the name of my current HTML document like this:
var path = window.location.pathname;
var page = path.split("/").pop();
console.log( page );
But now I want to be able to log the next HTML file in the series. So if I am on page2.html, I want to log page3.html. How can I replace that file name, with the next numbered file in my series ?
This should do it with pure javaScript:
var page = window.location.pathname;
console.log(page.replace(/\d+/g, parseInt(page.match(/\d+/g), 10) + 1));
Inner regex takes page number + 1 and the outer one replaces the value with it.
That's quite simple:
page=str.replace("page","");
a=parseInt(page);
a++;
console.log("page"+a+".html");
The first line raplaces the first part so it's "3.html" var. The second gets the integer out of page. What follows is an increment and an output.
That's one of the possibilities how it could be done.
Sources:
str.replace()
parseInt()
Thanks to #Mico and #SugarOnBacon this solution works!
var path = window.location.pathname;
var page = path.split("/").pop();
console.log(page.replace(/\d+/g, parseInt(page.match(/\d+/g), 10) + 1));

Use JS or jQuery to replace part of a URL

Ok, I am not sure what is wrong with me, but I am trying to find and replace a portion of multiple URLs.
Basically, I have some URLs that are being dynamically added to my site. All have a class of 'newsLink' some of the links are pulling up google.docs viewer and I need to remove that.
Here is my code thus far:
$('a.newsLink').each(function(){
var lnk = $('a.newsLink').attr();
var re = new RegExp("http://docs.google.com/viewer?url=","g");
lnk.replace(re, "");
});
the links look like:
<a href='http://docs.google.com/viewer?url=myHomePage.pdf' class='newsLink' target='_blank'>
I would like to remove the first part so that the link looks like:
<a href='http://myHomePage.pdf' class='newsLink' target='_blank'>
Anyway, no luck this far...can anyone please help.
First, you are getting all links again inside of the loop. Then, you try to get an attribute, but didn't say which one. Finally, you try to use replace without assigning the return value to anything.
This is what your code should be:
$('a.newsLink').each(function(){
var lnk = this.href;
this.href = lnk.replace("http://docs.google.com/viewer?url=", "");
});
Note: I'm assuming you want the links to become e.g. myHomePage.pdf, without the protocol.
The regular expression you want is.
http:\/\/docs\.google\.com\/viewer\?url=(.+)
First off, this escapes all regular expression characters. In this case \, ., and ?. We are capturing the document using a group that matches every character ((.+)).
So our code looks like this so far.
$('a.newsLink').each(function(){
var lnk = this.href;
var re = /http:\/\/docs\.google\.com\/viewer\?url=(.+)/g
this.href = lnk.replace(re, "");
});
Now we get the groups like so.
var match = re.exec(lnk);
This returns an array of the matches. Our document is now stored in match[1]. So our final code comes out to.
$('a.newsLink').each(function(){
var lnk = this.href;
var re = /http:\/\/docs\.google\.com\/viewer\?url=(.+)/g
this.href = (re.exec(lnk))[1];
});

javascript or jquery - get the last 2 characters of href

I need to get the last 2 characters from the href of a link and place them into a string.
I'm sure this is fairly simple but I seem to be struggling.
Here's the link
test
I need to grab the "bb" part of the href.
Presuming link is a reference to the element:
var chars = link.href.substr(-2);
If you need to get the reference to the link, it is best to give the link an ID attribute, e.g. <a href="../mypage/?code=bb" id="myLink">, where myLink is something that describes the link's purpose. You can then do this:
var chars = document.getElementById('myLink').href.substr(-2);
Finally, if what you want is the code parameter from your link, it may be best to parse the URL into parts. If there is a chance that your URL may be more complex that what you've shown, you should do real URL parsing. As Rahul has pointed out in his answer there are some jQuery plugins that perform this function.
try
$(function() {
var res = $('a').attr('href').split(/=/)[1]
alert(res);
});
This will not grab the last two character, but everything after the = sign which works probably more generic. And even if the <center> cannot hold, regex could look like
$(function() {
var href = $('a').attr('href'),
res = /\\?code=(\w+)/.exec(href);
alert(res[1]);
});
var href = $('a').attr('href');
var last2char = href.substr(href.length-2);
You can try for some querystring plugins which might be a better option.

Categories

Resources