Explanation of script defer attribute [duplicate] - javascript

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How exactly does <script defer=“defer”> work?
Can anybody explain me how defer works?
As example how following code will work:
document.write("<script id=__ie_onload defer " + ((location.protocol == "https:") ? "src='javascript:void(0)'" : "src=//0") + "><\/script>")
And why using different value of src for different protocol?

defer is an IE attribute that tells the browser to delay the execution of the script.
http://www.w3.org/TR/html4/interact/scripts.html#h-18.2.1
"When set, this boolean attribute provides a hint to the user agent that the script is not going to generate any document content (e.g., no "document.write" in javascript) and thus, the user agent can continue parsing and rendering."

Related

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

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

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.

javascript events not working with Chrome Extension , why ! :/ [duplicate]

This question already has answers here:
Port error while changing chrome extension from manifest v1 to v2
(2 answers)
Closed 8 years ago.
HTML // code
input id ="submit_btn" type="submit" value="find" onclick="goto();"
Javascript / code
function goto()
{
if (document.getElementById("s_keyword").value != "") {
var url = ("https://www.youtube.com/results?search_query=" + document.getElementById("s_keyword").value);
var site = window.open(url, '_blank');
site.focus();
}
};
it never enters into goto function !
Chrome extensions don't support inline events. Add the event listener in your JavaScript, and it'll be fine:
document.getElementById('submit_btn').addEventListener('click', goto);
You may want to avoid using goto as your function's name too. It may be a reserved keyword.

PHP $_Get and JQuery .load [duplicate]

This question already has answers here:
Encode URL in JavaScript
(22 answers)
Closed 8 years ago.
I want to load another page via Javascript like this
$("#resultarea").load("searchresults.php?searchstring=" + $("#searchbox").val());
in searchresults.php, a single statement
echo $_GET["searchstring"];
what I type in searchbox appears when searchresults.php is loaded except when I add space and another word, no thing appear at all. I heared it can be done by encoding or somewhat, I searched but didn't find a solution.
Try encodeURIComponent:
var encodedValue = encodeURIComponent($("#searchbox").val());
$("#resultarea").load("searchresults.php?searchstring=" + encodedValue);
Source
Demo

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

Categories

Resources