Refresh the page with javascript and GET variables - javascript

<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.

Related

Gmail: shortcut to filter messages like this

I would like to quickly access my Gmail emails sent by the same sender. That will save me some time and fluidify my workflow.
I'm dreaming of a shortcut to find those emails quickly and, unless I'm mistaken, there is unfortunately not such a default shortcut in Gmail.
Did anyone try to do the same, and if yes, how did you do it?
I'm trying to use Shortkeys Google Chrome extension by firing a JavaScript event, but for now I couldn't make it work:
All help is welcome.
i think it can help you:
var main = document.querySelector('div[role=main]');
var el = main.querySelector('span[email]');
var senderEmail = el.getAttribute('email');
var querySearch = 'from:(' + senderEmail + ')';
var queryUrl = 'https://mail.google.com/mail/u/0/#search/' + querySearch;
window.location.href = queryUrl;

Can't pass my parameters in iframe

I have a parent page with a form in an iframe:
https://profiel.pelckmansuitgevers.be/?email=dennis#hybridmedia.be
All the fields of the form should be prefilled. But that doesn't work anymore.
If you add the email parameter to the url, this parameter is added to the source of the iframe.
But on my iframe, I cannot get the email parameter.
I'm doing this in the iframe:
var urlParams = new URLSearchParams(window.location.search);
if (urlParams.has('email')) {
var email = urlParams.get('email');
//all my code here...
}
It seems that urlParams is empty.
But when I open the page in incognito mode (chrome) and do a hard refresh, all the fields are prefilled. So it works in this case.
Does anyone know what the problem is?
Maybe that my script is trying to receive the email parameter but that this doesn't exist at that moment? Or something else?
Thanks!
You seem to have 2 problems :
1) your iframe is in a subdomain, and you don't send the email parameter in the iframe url. So some browsers like Google Chrome won't be able to access the email.
Sample code for solving this problem :
var loc = window.location.toString(),
params = loc.split('?')[1],
iframe = document.getElementById('updateform');
iframe.src = "https://pelckmans.houston-1.hybridmedia.be/update/pelckmansuitgevers/" + '?' + params + '&v=' + Date.now();
2) JS cache problems
You can solve this with changing
<script src="js/prefill.js"></script>
with
<script>document.write("<script type='text/javascript' src='js/prefill.js.php?v=" + Date.now() + "'><\/script>");</script>
in your iframe page https://pelckmans.houston-1.hybridmedia.be/update/pelckmansuitgevers/
This problem happens often when you develop JS and includes this in a .js file.
The browser caches the js...a way to solve this is for instance to add a timestamp after the js filename, or a version number like i did in my code.
You can find more ways to do this in this thread How to append timestamp to the java script file in <script> tag url to avoid caching

add page title to javascript code

I have this javascript snippet which passes an id and the reffering page to a php page where i then collect the ip address, what i wanted to do is also capture the page meta title and also forward that to the php page. However i am very new to javascript and not sure if it is possible.
Any suggestions would be appreciated below is my javascript code, thanks.
var track = new Image();
track.src="http://testsite.co.uk/~vtrack/track/track.php?id=801003&self=" + document.referrer;
track.src = "http://testsite.co.uk/~vtrack/track/track.php?id=801003" +
"&self=" + document.referrer +
"&pageTitle=" + document.title;
document.title = `Your Title goes here`

Making A Dynamic HTML Page to Redirect to A URL Using JavaScript

I need to make A Dynamic HTML Page to Redirect to A URL Using JavaScript.As a beginner, i need your help...What i want to do is to redirect to a url through a html page.For example: suppose a page address is www.example.com/pages.html?http://yahoo.com/news so this pages.html page in this case will redirect a user to yahoo.com/news ....I know how to do this with PHP but i cant understand how i should do it with javascript and HTML . any idea? Thanks
This should do it:
function Redirect(){
var current = window.location.href;
var exclude = current.indexOf('?');
window.location = current.substr(exclude + 1);
}
setTimeout("Redirect()",5000);
You can use window.location.href="new-url.html"; to go to a URL with JavaScript.
To parse the URL, use var theURL=window.location.href.split("?")[1];
location.href = location.href.split("?")[1];

Create and go to url with Javascript

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;

Categories