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));
Related
This should be pretty easy but I'm struggling.
I have a button that fires a function. I want an alert to fire as well that tells me which page the user was on.
www.whatever.com/thispage1/whatever
www.whatever.com/thispage2/whatever
www.whatever.com/thispage3/whatever
So after my button is clicked, I want an alert that reads back "thispage1" or "thispage2" etc. I do not want the entire URL fed back to me. Is there a way to find text in a url based on its position or number of characters before it starts?
Look at window.location.pathname and use str.slice to extract the bit you want, with str.indexOf to find the indices to start/end at
var top_dir = window.location.pathname.slice(
1,
window.location.pathname.indexOf('/', 1)
);
Maybe this will help you get started. Key players here are window.location.pathname and string.split()
var returnPage = function() {
var urlString = window.location.pathname;
var stringArray = urlString.split("/");
return stringArray[0]; // number == whichever piece of the array you want to get
};
function myFunction() {
alert(returnPage());
}
I have some Javascript that parses out the name of a site so that I can query an XML file to pull data where the node's attribute is the last part of a URL.
<script>
function myExampleSite()
{
var myURL = window.location.href;
var dashIndex = myURL.lastIndexOf("-");
var dotIndex = myURL.lastIndexOf(".");
var result = myURL.substring(dashIndex + 1, dotIndex);
return result;
}
var exampleSite = myExampleSite();
</script>
For example, if the site is http://myexamplesite.com/status-Blah00 I would be able to get all data out of the Blah00 XML node and populate various aspects of the site with whatever is in the XML.
This works fine and I am able to use the URL name (Status-Blah00, Status-Blah01, etc.) to query XML against it and populate elements on the page based on the name of the site.
However I ran into problems where a site has a second - in the URL.
For example:
http://myexamplesite.com/status-Blah01-Blah00.htm
It should be parsing the Blah01-Blah00 node of my XML, but instead of just gets the data from Blah00 since it doesn't recognize the first -. I'm new to javascript and I'm confused as to how to basically do:
if 1 "-" in url then get last index
else the number of "-" in url is > 1, get first index.
How would I be able to count the number of "-" in the URL and logically do just that with the above Javascript?
You could use a regex for this problem. Here is a start:
"status-Blah01-Blah00.htm".match(/([^-]+)/g)
That code generates the array:
["status", "Blah01", "Blah00.htm"]
So you can work with the length of that array to find out how many hyphens are in the url.
Even easier: "status-Blah01-Blah00.htm".split('-') returns the same array.
Here is a single line with sequential regexes that can handle dashes occurring elsewhere in the url and that keeps the Blah01-Blah00 node as a single string rather than separating them, as it seems you requested.
"http://www.site-name.com/folder-name/01-10-20/status-Blah0100-Blah01.htm".match(/-([^.\/]+)\.htm/g)[0].match(/[A-z0-9][A-z0-9\-]+/g)[0]
Generates:
"Blah0100-Blah01"
Hello I'm new to javascript and I need a little help with regex/replace
I want to take a url for example (url case 1)
http://url.com/_t_lastUpdate_2670619?location=50457347
or (url case 2)
http://url.com/_t_2670619
I want to select in this case just
_t_2680619
Ignore everything after "?" and before (underscore)t(underscore)
Is it possible with regex/replace? the only thing I managed to do was select numbers only with
var _t__and_number = document.URL.replace(/[^\d]/g, '');
alert(_t__and_number);
But that doesn't solve my problem if there's something like url case 1
(if I could get just the first number even without the (underscore)t(underscore) it would already help me a lot.
Thanks
Solutions:
onetrickpony/michaelb958:
var _t__and_number = window.location.pathname;
alert(_t__and_number);
ermagana:
var _t__and_number = document.URL.replace(/\?.*/g, '').match(/.*\/(.*)/)[1];
alert(_t__and_number);
As suggested by onetrickpony, if this is the URL you are currently browsing, window.location.pathname will yield that part of the URL.
First Part like case (1) and (2)
var s = '//url.com/_t_522121?location=50457347';
var n = s.lastIndexOf('/');
var result = s.substring(n + 1);
alert(result);
n = result.indexOf('?');
result = result.substring(0, n);
alert(result);
If you're trying to pull out the value after the last / and before the ? you could try something like this
url.replace(/\?.*/g, '').match(/.*\/.*(_t_.*)/)[1]
The replace removes everything from the ? and forward, the match creates a group of the values found after the last forward slash which is then accessed by the index 1
Hope this helps.
here is the jsfiddle to show it works:
My JsFiddle
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);
}
here's the code:
function chatWin(url,name){
var nw;
var splitUrlResults = url.split("#");
url = appendDataWakeNVPs(splitUrlResults[0]) + '#' + splitUrlResults[1] ;
nw=window.open(url,name,"height=600,width=433,left=100,top=100,resizable=yes,scrollbars=no,toolbar=no,status=no");
if (nw.focus) {nw.focus();}
}
and then the link in the code:
PROD_TAB_EXPRT_LNK_EMAIL=javascript:chatWin('/customerService/contactUs/help.html#1','Help')
but the rendered code is:
<a href="javascript:chatWin('/customerService/contactUs/help.html#0#1','Help');">
Every link is getting #0 appended before the tab identifier- (#1 in this case).
thoughts?
It looks to me like the only way url ends up with #0#1 on the end of it is if appendDataWakeNVP() is appending #0 onto it's return value and your code is then adding the #1 onto the end of that.
So, I think your problem is in appendDataWakeNVP(). I'd suggest stepping into that function in your favorite debugger and you can discover what it does. Or grep for it in your source tree and examine it in your editor. If it's suppose to add #0 onto the end and you can't change that, but you don't want that there, then you will have to remove that before appending your own hash onto the end of it.
Any hash value you don't want can be removed with this:
url = url.replace(/#.*$/, "");
So, you could change this line of code:
url = appendDataWakeNVPs(splitUrlResults[0]) + '#' + splitUrlResults[1] ;
to this:
url = appendDataWakeNVPs(splitUrlResults[0]).replace(/#.*$/, "") + '#' + splitUrlResults[1] ;