get requested page url javascript - javascript

I know I can get the current URL in javascript with window.location.href, however is it possible to get the requested page URL as the user is leaving that page? I want to send it to ajax in the window.onunload function and do something with it in my session variables. Thanks

If a user is leaving your page via one of the links on your website, you can get the href attribute value with Javascript, but if he decides to type in new url address in address bar and hit enter, you won't be able to get this information using Javascript.

Related

How to Redirect User to Another URL Depending on the URL He Came From

Can a javascript code that runs when a page loads or equivalent solution detect which pior URL the user came from, and if the user came from a certain url then the javascript would forward the user to another defined url?
If you want to get url of the previous page: document.referrer

JavaScript - Determine how a user arrived at html page

I'm building a web application and for one of my pages, I need to determine how a user arrived at the page (i.e. by redirect or by directly typing in the URL).
Is this possible with vanilla JavaScript?
document.referrer.
You can also read Referer header on your backend server, because browser automatically includes it.
You can try using document.referrer
If somone types url in address bar it should return an empty string, otherwise if a link was clicked it should return prior page url

Find and print final redirect url using Javascript

Is there any way that I can use JavaScript to get the final redirect URL of a domain?
Let's say, I have a simple HTML webpage at the URL http://dummy.com/script.html containing the potential script which checks for the redirection of a user submitted URL. That is, if a user has submitted the URL http://example.com and it automatically redirects to http://example.org/abc.
Can we write a script that follows the redirection and prints out all the redirection URLs. Of course, my only interest is to get the final redirection URL and store it in a variable so that I can use further down.
Any ideas??

rerouting a URL with querystring through an anchor link

The setup is basically having Page A with anchor links as such
<a href="/index.php/iframe-wrapper?http://www.[desired link].com">
upon click, the URL is written as such in the users browser
http://www.[site].com/index.php/iframe-wrapper?http://www.[desired link].com
an iframe calls what comes after the querystring through javascript and displays it in the frame through
var query = window.location.search.slice(1);
basically being able to have URLs on Page A display in an iframe on Page B
im stumped on how to remove the querystring from the end result (maybe through htaccess?) (as it is modifiable on any user browser leading to all sorts of vulnerabilities)
if anyone would be able to help me out with htaccess or some other similar method, i will be deeply grateful
I'm not aware of any way that .htaccess could help decrease the chance of vulnerabilities. You're still accepting a query string and using that as the iframe source, which means anyone can still just submit any URL in the query string regardless.
A more secure way to handle it might be to create a database table containing all of the potential URLs. You could enter them manually, or if they change frequently you could have a secure form where authenticated users (or admins) can administer them.
Then in the query string of the link you can simply pass the id of the table row which contains the URL you want to use, and retrieve the appropriate URL for that ID server-side using PHP.
In this way, the iframe can only ever display one of the URLs that are stored in the database. Someone could still enter whatever they want in the query string, but it won't matter because if it's not the ID of a valid URL you can display an error message (or a default URL or whatever other fallback behavior you want).

Using javascript to create a value in url and submit that value via form?

I have a site that request that they could send out different urls to clients to track what links are being used. I told them to use google analytics but they are requesting to stay away from it.
What they are asking is they want to send a url to there customers such as,
http://www.yoursite.com/?link=Nameoflink
They want to get that cookie and set it.
Then when the contact form is used they want to be able to submit that link name with the form submission to show what links are being used to go directly to there site.
I was told this is possible but i have no knowledge of that custom of javascript or cookie expertise... =/
You can get the value of the params passed in through the url with location.search. To get the value of the param, use the location.search and then find the specific url value, then set that in another hidden text field or something...
if (location.search){
var search = location.search.substr(1).split("&"),
url = search.split("=")[1];
document.getElementById('hiddenInput').value = url;
}
Note- the code above assumes that your search string only contains the URL value & that the URL is first. If not, it is likely this will fail. You can update the code to account for that by checking to make sure that search.split("=")[0]==="url" or expanding it to parse out all of the search params into an object that you can reference by key.
Yeah, google analytics would make this a lot easier, especially if you had a specific page that would serve as a drop-point, telling you how many people click this special link.
Without google analytics, you could get the GET variable values via a PHP or ASP page script and have them set that way, or you can use soley JavaScript to take care of cookie setting and retrieval.
For JavaScript, these links should point you in the right direction:
JavaScript cookies:
(I can only post one link, but check out W3C School's article on JavaScript cookie handling)
Extract GET values via JavaScript:
http://www.go4expert.com/forums/showthread.php?t=2163

Categories

Resources