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

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.

Related

Defining a defualt value for my javascript function parameter will raise the following error on IE " SCRIPT1006: Expected ')'" [duplicate]

This question already has answers here:
Javascript Functions and default parameters, not working in IE and Chrome
(5 answers)
Closed 5 years ago.
I have the followig function inside my javascript file:-
function showOrHideField(curField="") {
where i am defining a function which take a parameter with defualt value "", now this is working well on Firefox. but on IE11 i got the following exception:-
SCRIPT1006: Expected ')'
so can anyone advice on this please?
Thanks
IE11 does not support default parameters
function showOrHideField(curField="") {
would need to be
function showOrHideField(curField) {
curField = curField || "";
//or
curField = curField!==undefined ? curField : "";

Javascript function parameter won't load [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 6 years ago.
I'm animating an svg file, and in order not to bloat my code I have written the follow function:
function animateSingleAtt(svgName, element, attToAnimate, attValue, animationTiming) {
svgName.select( element ).animate({
attToAnimate : attValue
}, animationTiming);
}
All parameters are loaded just fine, except for attToAnimate. I have no clue as to why this is – it is just passed to the svg element as an attribute named 'attToAnimate'.
I have tried logging it outside of the animate-function, and when I do that, it's passed just fine.
Any help will be greatly appreciated!
It looks like you are using attToAnimate as a key on an object. To do this properly, change the code to something like this:
function animateSingleAtt(svgName, element, attToAnimate, attValue, animationTiming) {
var anObject = {};
anObject[attToAnimate] = attValue;
svgName.select( element ).animate(anObject, animationTiming);
}
This should properly build the object you want to pass to .animate

How to add and retrive additional data to serialize() in jquery ajax? [duplicate]

This question already has answers here:
jQuery post() with serialize and extra data
(9 answers)
Closed 8 years ago.
I'm trying to work on how to append some more values to the serialize method in JQuery.
snippet
//
var dataString = $("#myform").serialize();
dataString.push({name:"type", value: "myvalue"});
//
On passing the values to ajax call page
I am using this additional data for my conditional check as
if ( $_POST['type'] == 'myvalue')
{
// on success code
}
else
{
//On fail Code
}
But the condition is always going to else but now in if condition, Please assist If I am missing something and taking it wrong.
**NOTE the code is working fine if I am passing this name and value as hidden field form form ...
Use .serializeArray()
var dataString = $("#myform").serializeArray();
dataString.push({
name: "type",
value: "myvalue"
});

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