Parse URL with Javascript [duplicate] - javascript

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I parse a URL into hostname and path in javascript?
I’m trying to parse the url of the page. For example the current page is location.href.
So the main page of my blog, if I use alert(location.href); it will return “http://diaryofthedead.co.cc/” in an alert box. If I use alert(location.href); on page two of my blog, it will return “http://diaryofthedead.co.cc/page/2” in an alert box. Is there any way to parse the URL to get the number at the end. Does anyone know how I could do that? Could I use wildcard or something, to do something like: location.href+”page/”+*; While * is equal to whatever follows “page/”, and then turn * into a variable?

You can use
var pagenum = location.pathname.match(/\/page\/(.*)/)[1];
It will extract anything past '/page/' in your URL;

Checkout this package jsuri
Or keep it simple http://james.padolsey.com/javascript/parsing-urls-with-the-dom/

URI.js is a library for working with URLs. It can not only parse URLs, but also offers a simple fluent API (jQuery like) for modifying URLs.

Take a look at the documentation on the location object http://www.w3schools.com/jsref/obj_location.asp
You first want the "pathname" part, location.pathname

Related

How to use a question mark in url?

Here's my question: How to use a question mark in url ?
For a school project I need to use "?version=1", "?version=2" and "?version=3" in my url for 3 different versions of a website but after some researches, I found that the ? is used to pass a query string and use it in server-side but I haven't found more informations or doc.
My problem is that we don't have server for the project and I would know if I can still the question mark and if it's possible to use it in Javascript in order to display specific elements on the page according to the version of the website I'm using.
Thanks !
You can still use the query string (?version=1) without having access to the server. Just capture the value using the method Austin linked and go from there.

How to pass a string which contain UTF-8 characters from javascript to PHP? [duplicate]

This question already has answers here:
UTF-8 all the way through
(13 answers)
How to store other languages (unicode) in cookies and get it back again
(1 answer)
Closed 5 years ago.
I'm building a references generator and I need to keep track of the generated references. To achieve this, I'm currently using Javascript to store a cookie containing the raw data, then a PHP page embedded on the result page takes the cookie and logs it into an HTML page, however, some characters appears like this : �. I really don't know which way to go to solve this (base64 encoding, unicode encoded characters...)
Here's the link to the website : http://louisfelixberthiaume.000webhostapp.com/source_siteweb.php
[It's in french]
I can't give you the link to the HTML page for obvious confidentiality reasons, however I'll provide a screenshot :
Generated references screenshot

Retrieve the complete URL from an iFrame [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How do I get the current location of an iframe?
Hi i want to get the complete URL, and with the methods i know i get the full url but of the iframe, and i want the 'final' document.location.href,
how can i get it?
i don't care if its php or js, but i'm guessing by my experiments that with PHp it's not possible...
If you want to know where you currently are:
Client side - javascript - you can use alert(window.location).
Server side - php - .$_SERVER["REQUEST_URI"] or $_SERVER['PATH_INFO'] depending on what you want.
Take a look at the top answer from this question.
It's important to note that it's (luckily!) not possible to get the URL of an iframe if the iframe's URL is from a different domain than the parent page. If you're trying to do that, you're probably out of luck.

persist value between two pages using javascript [duplicate]

This question already has answers here:
Persist variables between page loads
(4 answers)
Closed 7 years ago.
i have a one html page in which i am storing few user selected values now and want to use these values on another html page.
i am using jquery in my module and i have already tried window.name and window.localStorage but they don't persist values between two pages.
so please help me to solve this problem.
If you don't want a cookies--and if you're directing from the first page to the second, how about passing the values as GET variables to the next page:
http://example.com/newpage.html?var1=blah?var2=blerg
then you can access that data with window.location.search.
You could use the "hash":
http://my.app.com/page2.html#name1=val1&name2=val2
The hash would be ignored by the server, keeping things "clean". The second page can read the hash from
window.location.hash
and then parse out the name/value pairs with some simple string/regexp/array manipulation.
If you wanted the hash to be "hidden", your second page could also then remove the hash from the URL - this would not result in another trip to the server - changes to hash only result in browser/client side behaviour.
If it's just a few values, how about cookies?
Store the value inside a cookie on the first page and and retrive it on the second. Its very easy with the Jquery Cookie plugin http://plugins.jquery.com/project/cookie
You would have to try and use cookies (assuming users are nice enough to enable those).
Here is a very useful link: http://www.w3schools.com/JS/js_cookies.asp
Finally i got pretty cool solution on
http://plugins.jquery.com/project/DOMCached

Regex for valid URL (Javascript) (with a twist) [duplicate]

This question already has answers here:
Javascript regex to find a base URL
(2 answers)
Closed 6 years ago.
a simple regext that can test
Either but atleast one of (http, https, or www).
I've seen examples that has compulsion of protocol, or allows directly like google.com.
but for users, they are used to typing www., not all would type the protocol.
But still it should be a valid one, and not a "abced.com"
https://regex101.com/#javascript
there use (option1|option2|option3)
If you just need to test if the url starts with http, https, www then you can simply use /^(https?|www)/.
If you need a full regex for the url then check out Mathias Bynens post on the matter https://mathiasbynens.be/demo/url-regex and pick the one most suited to your needs.

Categories

Resources