qts on javascript, jquery [duplicate] - javascript

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Request Address in Javascript
Is there any method to retrieve the URL of the current page in JavaScript or jquery?
Please help.

window.location.href
Here is how you would do it in jQuery :P
(function($) {
$.getUrl = function() {
return window.location.href;
}
})(jQuery);
jsFiddle.
Warning: Obviously the jQuery function is for LOLs. Don't really use it.

Yes, use location.href
alert(location.href)

Use the following
document.location.href

Use the
document.location.href

Related

Show Parameter Segment of URL using JavaScript [duplicate]

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

How do we convert jQuery $('div#item').html() in VanillaJS [duplicate]

This question already has answers here:
How can I use innerhtml in JavaScript?
(4 answers)
Closed 5 years ago.
I need to convert jQuery method to VanillaJS
$('div#item').html()
can convert .html() using innerHTML. But have the issue with $('div#item'). I used document.getElementById('div#item').innerHTML It's not working for me. Someone please suggest any better solution.
just use the actual id of the element you want to acess
document.getElementById('item').innerHTML
Try
document.getElementById('item')
You may use vanilla JS for this task
document.querySelectorAll('div#item')[0].innerHTML;
try this:
console.log (document.querySelector('div#item').innerHTML)
<div id="item">ololo</div>

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);

Is there a script in Javascript That Can Determine What Country a User is In? [duplicate]

This question already has answers here:
How to get visitor's location (i.e. country) using geolocation? [closed]
(12 answers)
Closed 8 years ago.
Would Javascript be good for this? Can JavaScript even do this? If Javascript is not good for this purpose, how about PHP?
jQuery version. conversion to standard Javascript is straightforward
$.get("http://ip-api.com/json", function(response) {
console.log(response.country);
});
You could use http://ipinfo.io API for this.
$.get("http://ipinfo.io", function (response) {
console.log(response.country)
}, "jsonp");
http://jsfiddle.net/Em6nU/

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