Show Parameter Segment of URL using JavaScript [duplicate] - javascript

This question already has answers here:
Get the values from the "GET" parameters (JavaScript) [duplicate]
(63 answers)
Closed 5 years ago.
So I am trying to get the last part of a link and show it in a div on the HTML.
I'm using JavaScript and HTML to perform this; here's what I have so far:
<script type="text/javascript">
var pageURL = window.location.href;
var lastURLSegment = pageURL.substr(pageURL.lastIndexOf('/') + 1);
document.getElementById("getLink").innerhtml = lastURLSegment;
</script>
<div id="getLink"></div>
And here is my link:
https://playmafia.000webhostapp.com/world/play/create/?id=m12345
However, after executing this, nothing happens or shows. And plus, this code, as far as I know, will retrieve "?id=m12345".
I am new to javascript so any help would be great in figuring this out. Also, this may need jQuery, I'm not completely sure, so I will accept answers that involve it.
Once again, how can I get the parameter of the URL (in the example, m12345) to display in the HTML under the div with id "getLink"? Thanks to anyone who can help.

Try This :
var pageURL="https://playmafia.000webhostapp.com/world/play/create/?id=m12345";
var lastURLSegment = pageURL.substr(pageURL.lastIndexOf('/') + 1);
var value=lastURLSegment.substr(lastURLSegment.lastIndexOf("=")+1);
document.getElementById("getLink").innerHTML = value;
<div id="getLink"></div>

Try by changing document.getElementById("getLink").innerhtml to document.getElementById("getLink").innerHTML

I hope you need some changes in your code
document.getElementById("getLink").innerHTML = lastURLSegment;
TRY THIS ...

Related

Displaying value transferred from URL via javascript [duplicate]

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)

$getJSON not working - undefined is the result [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 8 years ago.
Here is the JSON I am trying to parse
{"sites":{"1":{"id":1,"company":"facebook","username":"abc#gmail.com","hint":"mascot"}}}
Here is the javascript parsing it. It was working then the structure of the JSON changed and I cannot for the life of me get it work. The result is 'undefinedundefined' which means that it does not understand what username and hint are. Any thoughts?
$.getJSON('http://localhost:3000/sites.json', 'limit=30', processWebsites);
function processWebsites(data) {
var infoHTML='';
$.each(data, function(website, websiteDetails) {
infoHTML+= websiteDetails.username
infoHTML+= websiteDetails.hint;
});
$('#info').html(infoHTML);
}
and finally the HTML
<body>
<div id = "info">
</div>
</body>
i needed a .sites after data. this fixed it.
I believe felix is correct above. If you just console this, you'll get the value you need.
update: add your breaks
var infoHTML = [];
var data = {"sites":{"1":{"id":1,"company":"facebook","username":"abc#gmail.com","hint":"mascot"}}};
jQuery.each(data.sites, function(){
infoHTML.push(this.username + '<br>' + this.hint);
});
If you happen to get more than one grouping, then just join them with 2 br's if you want some seperation.
$('#info').html(infoHTML.join('<br><br>'));
I am sure there are much more eloquent ways. this is just fast and dirty.

jquery - how to add an variable as an id in jquery command.? [duplicate]

This question already has answers here:
How to use JavaScript variables in jQuery selectors?
(6 answers)
Closed 8 years ago.
Its a simple question, could not find an answer from google.
Code
$.click(function(){
var curID = $(this).parent()[0].id;
$("#"+curID input).attr("checked",true);
});
Onclick function is giving me the parent id, Now, using the parent id i want to find the input element and add checked attribute.
I am not sure of the syntax of querying by dynamic ID.
I want to know how can i query by dynamic variable.
Thanks in advance.
$("#"+curID).find('input').attr("checked", true);
Or
$(this).parent().find('input').attr("checked", true);
Or
$('input', $(this).parent()).find('input').attr("checked", true); // using the scope argument
The selectors are strings... So should be handled as strings by concatenating the variables: and texts
$.click(function(){
var curID = $(this).parent()[0].id;
$("#"+curID+" input").attr("checked",true);
});
Your search is probably too specific. Break tasks down into their components instead of looking for a complete solution to a very specific problem. You are just dealing with basic string concatenation here.
You want:
var selector = "#foo input";
You have foo in a variable.
var selector = "#" + foo_variable + " input";

Simple Javascript Replace not working [duplicate]

This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 3 years ago.
This seems so simple and trivial but it is not working. Here is my javascript:
var url = "/computers/";
console.log(url);
url.replace(/\//gi, " ");
console.log(url);
And here is the output in my browsers console:
/computers/
/computers/
As you can see nothing changes. As you can tell from the code I'm trying to replace the forward slashes with spaces. What am I doing wrong?
url = url.replace(/\//gi, " ");
Nothing changes because you're not assigning the result of the replacement to a variable. Add url = url.replace()
url.replace(/\//gi, " "); returns the resulting string (in javascript you can't modify an existing string), you are not assigning it to anything
assign it like so:
url = url.replace(/\//gi, " ");

Replace function not replacing [duplicate]

This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 8 years ago.
I followed some documentation to use the JavaScript replace function and it's not changing anything. No errors are thrown. Any idea what I'm doing wrong? The variable is retrieved from XML - maybe it needs to be cast as a string or something?
for (var i = 0, iln = projects.length; i < iln; i++){
var thumb = projects[i].get('thumb');
thumb.replace("200.jpg", "640.jpg");
console.log(thumb) //200.jpg not replaced
}
The full thumb value should look like this:
http://b.vimeocdn.com/ts/160/895/160895498_200.jpg
Is there a better way to find and replace things?
Assign the value back into thumb.
thumb = thumb.replace("200.jpg", "640.jpg");
Try:
thumb = thumb.replace("200.jpg", "640.jpg");

Categories

Resources