This question already has answers here:
Get the values from the "GET" parameters (JavaScript) [duplicate]
(63 answers)
Closed 6 years ago.
Is it possible to change a javascript variable through the URL?
Here's an example of the code I'm trying to modify from a website. (www.example.com)
<script type="text/javascript">
var x = 0;
</script>
I want to change the variable x from 0 to 1.
I want to do this by appending something to the URL. I'm not sure about the syntax, but I think it may be something like this:
www.example.com#javascript: var=1;
Is it possible to change variable x by only modifying the URL?
EDIT:
The duplicate question doesn't tell me how (if it's possible) to change the variable through the URL. Please let me know if that's not the case.
Related Question:
https://security.stackexchange.com/questions/134240/modify-javascript-variable-with-url-exploit
you can use:
if(window.location.href.indexOf("your_link_to_check") > -1) {
var x = 1;
}
Related
This question already has answers here:
How can I access object properties containing special characters?
(2 answers)
Closed 9 months ago.
An API returns a string of text that looks like this (xxx used for security):
{"xxx":{"xxx":{"xxx":{"xxx":{"results":[{"latest.GigabytesIngested":12641.824682336}]}}}}}
If I do this:
console.log(JSON.parse(body).xxx.xxx.xxx.xxx.results[0]);
I get this, which is fine:
{ 'latest.GigabytesIngested': 12641.82487968 }
My problem is I only want to grab the number. The below attempt doesn't work, maybe because there's a dot in the key name, or maybe because I'm just doing it wrong?
console.log(JSON.parse(body).xxx.xxx.xxx.xxx.results[0].latest.GigabytesIngested);
#derpirscher answered correctly in a comment:
console.log(JSON.parse(body).data.actor.account.nrql.results[0]['latest.GigabytesIngested']);
Yes, the period in the key is the problem. You need to use an alternate way to reference the key.
console.log(JSON.parse(body).xxx.xxx.xxx.xxx.results[0]["latest.GigabytesIngested"]);
or
var result = JSON.parse(body).xxx.xxx.xxx.xxx.results[0];
var lgi = result["latest.GigabytesIngested"];
console.log(lgi);
This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 6 years ago.
If I have a input box with value 'jklmnop', how would I go about removing 'k' using javascript?
I was thinking something along the lines of:
<input type='text' id='letters' value='jklmnop'/>
<button onclick='removeK()'>Remove</button
Using this javascript:
function removeK() {
document.getElementById('letters').value.replace('k','');
}
Can anyone tell me why this doesn't work? And if so, what would I need to do to make it work?
Thanks :)
You aren't setting the value.
var l = document.getElementById('letters');
l.value = l.value.replace('k','');
This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 9 years ago.
Wondering if someone could help with out with creating a regex..
Basically, taking an iFrame's src, and seeing if it's from SoundCloud. If it is, return its id. For example:
var src = 'http://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F85110490&auto_play=false&show_playcount=false&show_user=false&show_comments=false&buying=false&liking=false&sharing=false&show_artwork=false&color=00e7ff';
function soundcloudId(src) {
var p = /___________/;
return (src.match(p)) ? RegExp.$1 : false;
}
soundcloudId(src);
And as a result, it would run the "src" through the regex, and if a soundcloud link, would return 85110490. Otherwise, false.
Try this regex:
/http:\/\/w.soundcloud\.com\/.*%2Ftracks%2F([0-9A-F]+)/
Runnable example: http://jsfiddle.net/mYf6P/
This question already has answers here:
Replace function not replacing [duplicate]
(2 answers)
Closed 8 years ago.
I have written a simple code in a .js file (in Zend framework, but I dont think it matters)
var location = "localhost:8080/mymodule/id/1#";
location.replace(/\#/g, "");
alert(location.valueOf());
return;
but I dont know why I can not see the result I want.
I get my page url and want to omit all number signs appears in it. but the code above does nothing.
please help
location is a bad name to use for a variable since it collides with the window.location variable used for the actual browser page location.
If you change location to loc in your above code, and then also add loc = in front of the loc.replace() call (since replace() doesn't modify the input, but instead returns the new version), your code works.
replace will not change the value of the original string, you need to assign the result to a new variable -
var newString = location.replace(/#/g, "");
alert(newString);
Demo - http://jsfiddle.net/5H5uZ/
It can be done in one line. This is the result you look for?
alert("localhost:8080/mymodule/id/1#".replace(/#/g,''));
//=> alerts 'localhost:8080/mymodule/id/1'
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to rewrite URL without refresh, like GitHub.com
let say I have variable in Javascript
var currentpage = 0;
my address page address is www.xyz.com/page/1
when I click somewhere and js function trigger
currentpage = 1
//Here I want to change/rewrite www.xyz.com/page/2
//Without redirecting to this page
See pushState (browser support currently limited).
You can't. You have to use the '#' or '#!' notation and pass the page number after it, then do trigger some js on load to figure out which page to display.