Displaying value transferred from URL via javascript [duplicate] - javascript

This question already has answers here:
Get the values from the "GET" parameters (JavaScript) [duplicate]
(63 answers)
Closed 3 years ago.
I'm having an issue with my codes concerning the transfer of information from one page to another using the url as shown below:
window.location.href ="form.html?uname="+uname;
The value is displaying in the url box but when I try to display it on the form.html page using the following code:
window.onload = function ()
{
var name = document.getElementById("uname");
alert(name);
}
The alert keep displaying null.
What is the issue because after an hour of troubleshooting, I can't seem to figure it out.
Is the null being displayed in the alert box means that the value is not being retrieve from the url?
Thanks in advance.

document.getElementById('uname')
looks for an HTML element with the corresponding id. What you probably want to do is:
alert(uname)

Related

How to make a script that will generate a url by adding the input provided in form [duplicate]

This question already has an answer here:
Form Input to Change URL
(1 answer)
Closed 9 months ago.
This post was edited and submitted for review 9 months ago and failed to reopen the post:
Original close reason(s) were not resolved
I want to make a javascript that will redirect to the url that will be generated by adding the input provided in form and the main url.
For example ,
If the user puts 'Hello' inside the textbox
Then it should redirect it to 'https://example.domain/Hello'
And if he puts Bye then it should redirect it to 'https://example.domain/Bye'
You can simply grab the value of the textinput and append it to your baseurl
const inputBox = document.getElementById("input");
let newURL = 'https://example.domain/' + inputBox.value;
Please supply some Code when posting Questions to SO. Follow the How to guide https://stackoverflow.com/help/how-to-ask

External JSON file string to HTML output [duplicate]

This question already has answers here:
read an external local JSON file into Javascript
(4 answers)
Closed 3 years ago.
I have an external JSON file on my server that I want to display the objects from into an HTML document without using JQuery. I specifically just want to call the username object and display it on an "All Users" page for a simple homework project. I've had a difficult time finding ways to do this without JQuery. How could I go about doing this?
JSON Example:
{"name":"asfd","username":"awsf","email":"kean","age":"21","gender":"Male","submit":"Submit"},
{"name":"asdf","username":"asfd","email":"asdf#asdf","age":"21","gender":"Male","submit":"Submit"},
{"name":"null","username":"null","email":"null#gmail.com","age":"21","gender":"Male","submit":"Submit"},
{"name":"null","username":"null","email":"null#gmail.com","age":"21","gender":"Male","submit":"Submit"},
If you have an array of json objects you can loop over it and call JSON.parse() on each. That will give you simple user object that you can access and place in your page his username.
for(var i=0; i<users.length; i++) {
var user = JSON.parse(users[i]);
// you can now access username for each user with user[i].username
// place a username into new element in your page
}
Useful reference for making server call without jQuery : How to make an AJAX call without jQuery?

Get the query string value from a URL using javascript [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 6 years ago.
I have a URL like
http://something.com?acess_token=ashv6786sdjfhjd
When I click on that link, how to get the access_token from that link using Javascript?
Try :
var Yoururl= "http://something.com?acess_token=ashv6786sdjfhjd";
var acessToken=Yoururl.split('?')[1].split('=')[1];
Working Fiddle
For url parameter see old so question from given link.
Get url parameter via jquery
If you want the value of the access_token you mean this:
ashv6786sdjfhjd
Then here's a quick solution. Hope it helps!
var str = "http://something.com?acess_token=ashv6786sdjfhjd.when";
var accessToken = str.split("=")[1].split(".")[0];
console.log(accessToken);

How to take a value from a link using jquery [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 7 years ago.
I want to pass one value through ajax by taking the values from jQuery. But I am using link so I have problems taking the value. I tried the following,
<a id="addpa" class="ActionPopup" href="http://localhost:49951/admin/assignhome/Add?sPId=7">Add</a>
Jquery Code:
var spId = $("#addpa").prop("href"); // Here i am getting a whole Url
var thequerystring = getParameterByName("sPId");
The result is showing undefined. How to take the value of sPId? Give me ideas..
How to take the value of sPId?
Try using String.prototype.split() , Array.prototype.pop()
var spId = $("#addpa").prop("href").split(/=/).pop();

How do I read #2 at the end of a URL? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can you check for a #hash in a URL using JavaScript?
Currently I am generating Urls that look like this:
InspectionPhotos.aspx?inspectionId=10001649#/2
The #2 is for a photogallery plugin, and this would mean go the second photo.
I would like to show a div only if there is a #/[anynumber] but if its just
InspectionPhotos.aspx?inspectionId=10001649
then not show anything.
How could I do this check? Either asp.net on pageload or a client side javascript would be fine.
Thanks.
You can't do this in server side, because the hash is not sent to the server, to get this value with javascript is simple:
var hash = window.location.hash;
if (hash){
//use the hash value.
}
JavaScript: window.location.hash will comeback with #/2 from your example.

Categories

Resources