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

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
}

Related

How to check for a specific word in the URL before running script? [duplicate]

This question already has answers here:
How to make a script redirect only once every time an appropriate page loads?
(4 answers)
Closed 3 years ago.
I have the current script to amend a link in the URL bar
(function() {
'use strict';
location.replace(location.href.replace("www.reddit.com", "old.reddit.com"));
})();
I'd want it to check if the URL has the word "old" before running the remainder of the script. How do I do this?
You can check the index of the word old by considering the URL as a normal string.
if(location.href.indexOf('old') > -1) {
// do something
}
It is simple as that.

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

the javascript code works but I get an error [duplicate]

This question already has answers here:
Set a default parameter value for a JavaScript function
(29 answers)
Closed 5 years ago.
I have created the following javascript code. This code is working fine but dreamweaver say, line (function load_unseen_notification(view = '')) something wrong. But what is the problem here code is working fine. I think the problem will come view ='' . How can i fix it?
function load_unseen_notification(view = '')
{
$.ajax({
url:"fetch.php",
method:"POST",
data:{view:view},
dataType:"json",
success:function(data)
{
$('.dropdown-menu').html(data.notification);
if(data.unseen_notification > 0)
{
$('.count').html(data.unseen_notification);
}
}
});
}
DreaemWeaver is not using ES6 (which introduced default values for parameters) by default. But you can set this behavior in the settings. Just take a look at it.
JavaScript before ES6 doesn't support default values for parameters. But you can rewrite it to if you don't wnat to change the settings (which would be recommend):
function load_unseen_notification(view) {
view = view || ''; // if view is defined, use the value. If not set view to an empty string.

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

JS: Append variable to URL without repeating [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Change URL parameters with jQuery?
Currently, I have a <div> tag that looks like a button and changes the URL with an onClick function which redirect to the same page just adds &view_all=Yes or &view_all=No to the URL.
<div onclick="javascript:window.location.href = 'page.php?action=list-volunteer-apps&view-all=No';">Hide Closed Applications</div>
Well, I am adding a new feature to the website and I need to be able to append that &view_all=Yes or &view_all=No to the end of the URL instead of redirecting them because I'll have other variables in the URL that I can't lose.
I have figured out a way to do the append but it keeps adding the &view_all variables to the end of URL so it looks like this page.php?action=list-volunteer-apps&view-all=No&view_all=Yes&view_all=No.
This is the way I am doing the append:
onclick="javascript:window.location.assign(window.location.href+='&view-all=No');"
You can use regular expression to replace the value in a string:
$(this).attr("onclick", function(i, val) {
return val.replace(/view-all=(Yes|No)/, function() {
return "view-all=" + ((arguments[1] || "") == "Yes" ? "No" : "Yes");
});
});

Categories

Resources