I want to be able to produce a URL based on certain properties and then go to the new URL in javascript.
Here is what I have so far:
triggerNumber = document.findcontrol(txtTrigNo).text;
hostAddress= top.location.host.toString();
url = "http://" + hostAddress "/" + triggerNumber
How do I navigate to the new URL?
Simply try:
window.location = url;
But before trying to do that, you have to make sure the page at the address "http://" + hostAddress "/" + triggerNumber exists. For example by putting valid triggerNumbers in an array and check if it exists or not. So:
//Not sure if at the end it should be .text or .value or .value()
triggerNumber = document.findcontrol(txtTrigNo).text;
var validTriggers = [123, 456, 789];
if (validTriggers.indexOf(parseInt(triggerNumber)) == -1) {
alert("Invalid trigger number");
} else {
hostAddress= top.location.host.toString();
url = "http://" + hostAddress "/" + triggerNumber;
}
Finally, if the destination is a server-side page (php, asp, etc), the address usually looks like this:
"http://" + hostAddress "/trigger.php?id=" + triggerNumber;
but you'd better use forms for this.
Edit: As Cerbrus suggested, validating the values with javascript is a good way to tell the user about his errors before navigating away from the page. But to make sure the correct data is sent to server, it is important to do the validation in the server-side code, too.
In this example, in case of an invalid trigger number the user may finally see a 404 error; but with sensitive information worse things can happen.
What you need is:
document.location.href = url;
After you have the URL in the url variable.
To get value of input element have:
var triggerNumber = document.getElementById("txtTrigNo").value;
This will get the hostname and port of the server, and concatenate the value of the element onto the end, and then go to the resulting URL.
var triggerNumber = document.getElementById("txtTrigNo").value();
var url = "http://"+window.location.host+"/"+triggerNumber;
window.location = url;
Related
I have this code :
if($('#category').val() == 4){
console.log("http://"+window.location.hostname+'/dailyGift?id_event='+$( "#sub-category" ).val()+'?week_id='+$('#week_id').val()+'?year_id='+$('#year_id').val());
window.location = "http://"+window.location.hostname+'/dailyGift?id_event='+$( "#sub-category" ).val()+'?week_id='+$('#week_id').val()+'?year_id='+$('#year_id').val();
}
In the console I have
http://myWebsite.dev/dailyGift?id_event=41?week_id=44?year_id=2016.
When I access directly works without problems, but jQuery does not make this redirect and I don't understand where is the problem.
You need to change all the ? with & except the first one
if($('#category').val() == 4){
console.log("http://"+window.location.hostname+'/dailyGift?id_event='+$( "#sub-category" ).val()+'&week_id='+$('#week_id').val()+'&year_id='+$('#year_id').val());
}
Please replace all "?" with "&", Correct URL would be :-
http://myWebsite.dev/dailyGift?id_event=41&week_id=44&year_id=2016
Also it would be good if you encrypt ids for security purpose.
You say that you want to redirect to a link but you're merely changing the location.href value which is equivalent to clicking a link.
Location.replace() will load the new resource in place of the current one. The current page will not be added to the session history so it will not be possible to return to it using the back button.
I've also cleaned up the use of " and ' in the url string to make it cleaner.
if ($('#category').val() == 4){
var url = "http://"+window.location.hostname+"/dailyGift?id_event="+$('#sub-category').val()+"&week_id="+$('#week_id').val()+"&year_id="+$('#year_id').val();
console.log(url);
window.location.replace(url);
}
I am new to javascript, and stuck on this problem. I am trying to have the url query string change based on which checkbox checked. But I seem to have some issue with window.location, Some of the JavaScript is below
var urltest= window.location.pathname;
var urltest2 = window.location.host;
var currentURL = urltest2 + urltest;
url = currentURL + "?" + arr_opts.join('&');
document.getElementById('myurl').innerHTML = url;
//window.location.href = url;
window.location = url;
The window.location does not work here, but when I change var currentURL to
var currentURLL = window.location.href;
it is work, but not with
var urltest= window.location.pathname;
var urltest2 = window.location.host;
var currentURL = urltest2 + urltest;
I need window.location to point page to the currentURL above.
Any help would be appreciated.
You're not providing a protocol, so the location.href change is being treated as "go to this relative path from the current location", i.e. on this page
window.location = window.location.host + window.location.pathname;
// takes us to
// http://stackoverflow.com/questions/35254564/javascript-window-location-not-working/stackoverflow.com/questions/35254564/javascript-window-location-not-working/35254601
Do one of the following
Provide a protocol so it knows it is an absolute URI,
window.location = window.location.prototcol + '//' + window.location.host + window.location.pathname + '?foo=bar';
Tell it to re-use the current protocol but work as an absolute URI
window.location = '//' + window.location.host + window.location.pathname + '?foo=bar';
Provide an origin (instead of host)
window.location = window.location.origin + window.location.pathname + '?foo=bar';
Tell it to re-use the same origin but work as an absolute path
window.location = window.location.pathname + '?foo=bar';
Just update the query
window.location = '?foo=bar';
Always choose the most simple option to make your life easier if you ever need to debug, i.e. if you can assume you will always want the same protocol, host and path, just update the query.
Useful knowledge
Starting a URL with..
// means same protocol
/ means same origin
? means same path
# means same query (will not re-load)
The pathname is going to be something like /questions/35254564/javascript-window-location-not-working, and the host is something like stackoverflow.com. If you put those together with code like yours, you get
stackoverflow.com/questions/35254564/javascript-window-location-not-working
That's clearly not correct; the host looks like part of the pathname. You can use protocol and port if you really feel the need to reconstruct the URL from its constituent parts, but using the href seems simpler if all you want to do is add a query string.
I'm trying to create a bookmarklet (something I've never done) that will read the URL and check if it ends with "/directory". If it does, I want to remove "/directory". If it doesn't, I want to add it.
I'm using the following to successfully append the directory to the URL, but I don't know how to check if it's already there or delete it if it is.
javascript: window.location = window.location.protocol + '//' + window.location.hostname + window.location.pathname + '/directory';
This will do it:
var URL = window.location.href; window.location.href = /\/directory$/.test(URL) ? URL.replace(/\/directory$/, '') : URL + '/directory';
Checks if the url ends with /directory if does, removes it, else adds it.
<script type="text/javascript">
var email = document.write(localStorage.getItem('email'));
var pass = document.write(localStorage.getItem('pass'));
var url = document.write(document.URL);
document.location.href = url+"?email="+email+"&pass="+pass;
</script>
But when I enter the page I left the url like this:
http://example.com/undefined?email=undefined&pass=undefined
Not happening ... Anyone know the problem? Thank you very much!
Well, what's up with document.write(…) in here? You don't want to print out anything:
var email = localStorage.getItem('email');
But if you want to print out the values for testing:
var email = localStorage.getItem('email');
document.write(email);
(See also console.log(…))
You should escape the parameters using encodeURIComponent(…):
location.href = url + "?email=" + encodeURIComponent(email) +
"&pass=" + encodeURIComponent(pass);
Also you should not use document.write anyhow. There are plenty more reasonable methods to change the content dynamically on you website.
You should not send a password using GET requests, as they will appear the browser, proxy and server logs. Use POST requests through invisible forms.
I have a few hyperlinks like this on my page
Link
When there is a query string in the address, example: mydomain.com?r=abcd the hyperlink should change to Link
I want the same thing to happen to "rh" query argument also. ie, when someone goes to mydomain.com?rh=abcd
This Link
should change to Link
Basically the script should say: if the queries "r" and "rh" is not null, the links with the class=rewrite must be changed. Everything after the "?" must be removed & the query string in the address should be added to the hyperlinks.
change the domain:
var newurl = 'http://testdomain.com';
$('a').each(function(I,EL){
var url = $(EL).attr('href');
if(url.indexOf('?')>= 0){
url = url.split('?');
url = newurl + url[1];
$(EL).attr('href', url);
}
}