localStorage.setItem() not working on page load [duplicate] - javascript

This question already has answers here:
How to execute a JavaScript function when I have its name as a string
(36 answers)
Closed 4 years ago.
I want to load a page and, after the body tag, execute a sort based on localStorage. But myWay (no pun intended) doesn't work. Can you help me?
I have 3 sort functions, each with one of these lines:
<script>
localStorage.setItem("current_sort", "classSort()");
localStorage.setItem("current_sort", "playerSort()");
localStorage.setItem("current_sort", "ratingSort()");
</script>
<script>
myWay = localStorage.getItem("current_sort");
if (myWay === "") {myWay = "playerSort()";}
</script>
How do I load myway?
<script>
eval (myway()); //doesn't seem to work.
</script>

that is how we execute functions as second parameter:
localStorage.setItem("current_sort", function(){classSort()});
localStorage.setItem("current_sort", function(){nameSort()});
localStorage.setItem("current_sort", function(){ratingsort()});

Related

Why is this javascript injection attack not working? [duplicate]

This question already has answers here:
Can scripts be inserted with innerHTML?
(26 answers)
Closed 7 years ago.
I am trying to find the right way to harden my Javascript against code injection attacks.
So, I created what I thought would be a successful code injection:
document.getElementById("result").innerHTML = "hello <script> alert(0) <\/script> kuku";
Evaluating document.getElementById("result").innerHTML in debugger shows that it did go through:
"hello <script> alert(0) </script> kuku"
So how come there is no alert?
Setting the .innerHTML to content that includes <script> blocks will never cause the code embedded to be evaluated. That's just how .innerHTML works.

Pass a variable from a javascript function to another php file [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 8 years ago.
i am new to javascript and php and i need your help. I have a javascript function inside a php file. In this javascript function i have a variable that i want to pass it to another php file. Can anyone help me how to do this.
Thanks
There are several ways you can do this. Probably the simplest way is:
var myData = 'whatever';
window.location.href = 'otherscript.php?myData=' + myData;
Then your script 'otherscript.php' can access that variable with $_GET['myData'].
This method requires a page refresh. You can do it without refreshing the page by using ajax, but that is a little bit trickier.
Something like that should work:
<script type="text/javascript">
function yourFunction(yourParameter) {
<?php $abc = "<script>document.write(yourParameter)</script>"?>
}
</script>
<?php
echo $abc;
?>

Run JS code only if a condition is met in URL [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 8 years ago.
I would like to run the following code but only if the condition is that the url contains "Mode=edit" somewhere. (If I run it on every page it gives error).
<script language='javascript'>
function Save() {
__doPostBack('ctl00$MainContentPlaceHolder$btnSave', '');
}
function Redirect() {
window.location = "SessionTimeout.aspx"
}
window.onload = function () {
setTimeout(Save,15000);
}
</script>
How can I have it check for that portion in the url and only then run this script above?
Just add an if statement that checks if document.location contains the string.
if (document.location.href.indexOf('Mode=edit') > -1){
//Your code goes here
}

chrome.tabs.executeScript - how to make a script return a value? [duplicate]

This question already has an answer here:
about chrome.tabs.executeScript( id,details, callback)
(1 answer)
Closed 8 years ago.
I am injecting a script into a tab using chrome.tabs.executeScript. Its last parameters is a callback function that will be called with The result of the script in every injected frame. . How do I make my script return a result? I tried adding a simple return statement at the end of my js, but nothing got returned.
As RobW corrected me, a function is not necessary, just make sure that the desired data is the last expression and of course that is JSON-serializable.
chrome.tabs.executeScript(tabId,
{code:"document.charset"},
function(results){
// results[0] will now contain the charset for the page in question
});

How to get the sheet object in the sheet properties? [duplicate]

This question already has answers here:
How can a JavaScript object refer to values in itself? [duplicate]
(8 answers)
Closed 9 years ago.
How to get the form object?
I use this.box meaning sheet.box here, but the script produces an error. How do I get the sheet object in the sheet properties?
<div class="box">
<form action=""></form>
</div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
var sheet={
box: $('.box'),
form: this.box.find('form') // TypeError: this.box is undefined
}
</script>
I guess what you need is a function:
form: function() {return this.box.find('form');}
Otherwise, you should be more specific.
Old versions of Firefox (I tried Firefox 4 which warns but still accepts it) actually allowed you to do this:
var sheet = {
box: #1= $('.box'),
form: #1#.find('form')
}

Categories

Resources