How to check passed function name as paremter - javascript

I have a javascript function that accepts a function as a parameter. I want to determine the name of the function is passed. How is that possible with javascript?
Here is a code I have so far. $fnc == "SliderMoney" is working. It's always returning false.
function SliderCreate($id, $fnc) {
$lbl = "#" + $id + "_lbl";
$div = "#" + $id;
$slider = $($div).slider({
});
if ($fnc == "SliderMoney") {
UpdateDisplay($slider, $lbl, "$");
} else {
UpdateDisplay($slider, $lbl, "");
}
}
Thanks,
Susan

No you can't by default.
Perhaps you pass it an object or as a third parameter.

Related

How do I insert the value of a variable within an HTML attribute using pure Javascript

I'm trying to insert a value that I'm getting from the url inside the HTML attribute, as follows:
I did this function
function urlId(qs) {
document.getElementsByTagName("BUTTON")[0].setAttribute("data-channel-external-id", "qs.channelid");
}
urlId(qs);
I want to insert the value of this variable qs.channelid (qs.channelid is the variable of a function that I did to get a value from the ex url:www.mysite.com/pagina.php?id=VALOR that I want Get and set within the HTML attribute.
  So I made this code above and I put the following in my data-channel-external-id="urlId(qs)" attribute but it is not working ...
You don't need quotes: "qs.channelid" is a static string, while qs is object and qs.channelid is its dynamic property, which value you want to read. So it will be:
function urlId(qs) {
document.getElementsByTagName("BUTTON")[0].setAttribute("data-channel-external-id", qs.channelid);
}
urlId(qs);
If qs is a function, you must call the function before you can access its properties
function qs() {
// Do something in your function and set channelid on this (the function itself)
this.channelid = "test";
return this;
}
function urlId(qs) {
var qsResult = qs();
// Call the function, after that, you can access its properties: qs().channelid
document.getElementsByTagName("BUTTON")[0].setAttribute("data-channel-external-id", qsResult.channelid);
document.getElementById('channelid').textContent = "Attribute data-channel-external-id=\"" + qsResult.channelid + "\" is set on the button";
}
urlId(qs);
<button id="example" value="example">example</button>
<label id="channelid"></label>
In regards to your comment, you can set the channelid at any point in your code:
function qs(url) {
// Do something in your function and set channelid on this (the function itself)
this.channelid = url;
return this;
}
// pass the url as a parameter
var qsResult = qs("the-url-you-want");
console.log("When using the parameter: qsResult.channelid === \"" + qsResult.channelid + "\"");
// or don't use a parameter and set the url later on
var qsResultWithoutParameter = qs();
console.log("When not using the parameter: qsResultWithoutParameter.channelid === undefined");
// Now set the channelid after the function is called
qsResultWithoutParameter.channelid = "the-url-you-want";
console.log("Setting the channelid after the function is called: qsResultWithoutParameter.channelid === \"" + qsResultWithoutParameter.channelid + "\"");

Same name for a string and a function

Right now I'm having a major brain fart. I have this function:
function uplodeVirus(){
console.log('working')
console.log('uplodeVirus')
var form = document.getElementsByTagName('form')[1]
console.log(form)
var select = form.children[0]
console.log(select)
for (x in select) {
var lN = select[x].innerHTML // var linkName
if (lN == "vspam 0.3 [OpenRelay-backdoor.vspam ] (0.003 Gb)"){
value = select[x].value
select[x].setAttribute("selected", "selected");
form.submit()
break
}
}
}
Don't worry its not a real Virus!!! This is a bot for a game called slave hack - for learning purposes
Anyways, when I call the function:
var ip = '2.2.2.2'
var uplodeVirus = 'http://www.slavehack.com/index2.php?page=internet&var2=' + ip + '&var3=files&aktie=upload'
var currentUrl = window.location.href
console.log(currentUrl)
console.log(uplodeVirus)
if (currentUrl == uplodeVirus) { //Yes, I made sure that currentUrl = uplodeVirus
uplodeVirus()
}
Nothing happens... but if I take the code out of the function and do this:
if (currentUrl == uplodeVirus){
console.log('working')
console.log('uplodeVirus')
var form = document.getElementsByTagName('form')[1]
console.log(form)
var select = form.children[0]
console.log(select)
for (x in select) {
var lN = select[x].innerHTML // var linkName
if (lN == "vspam 0.3 [OpenRelay-backdoor.vspam ] (0.003 Gb)"){
value = select[x].value
select[x].setAttribute("selected", "selected");
form.submit()
break
}
}
}
It works!!! Now, I could go with option B, but I really want to figure out what i did wrong.
Thanks in advance!
You are naming both a URL variable and a function with the same name: uplodeVirus
Since the variable is initialized to hold a string before you try to call the function, calling uplodeVirus() is the same as calling ("")(). It doesn't make any sense, because a string is not a function.
Try changing the name of one or the other.

RegExp Test with two variables : possible ?

I'm trying to use the Test() Method like I did here :
var namealbum = data.data[i].name;
var text = namealbum;
if (/les comics d/i.test(text) == false) {
//do nothing
} else {
var albumid = data.data[i].id;
$('body').append('<section id="album album'+j+'"><h2 id="album_title">'+text+'</h2><div id="images"></div></section>');
But I wonder if it's possible to use this method with two variables.
Kind of like this :
var comicname = photos.data[i].name;
var comicpicture= photos.data[i].source;
var comiclink = photos.data[i].link;
if (albumid.test(comiclink) == false){
}else {
$('#images').append('<img src="'+comicpicture+'"/>');
};
Actually, I want to test if there is the content of the variable albumid (which is the ID of an album) in the link of a photograph stored in the variable comiclink.
I hope you guys will understand my bad english.
Thank you though !
In this case there is no need to use a regex, You can String.indexOf() like
if (comiclink.toLowerCase().indexOf(albumid.toLowerCase()) > -1) {
If you still want to use a regex
//on page load
if (!RegExp.escape) {
RegExp.escape = function (value) {
return value.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&")
};
}
var regex = new RegExp(RegExp.escape(albumid), 'i');
if (regex.test(comiclink) == false) {} else {
$('#images').append('<img src="' + comicpicture + '"/>');
};

jQuery.find() doesn't work inside html variable

I have a very simple problem and keep finding answers to similar questions with more complexity. I am trying to replace image links in loaded html and decided that the best is to read the html into a string variable loadedHTML using .get(), like this:
$.get(loadURL, function(loadedHTML) {
myFunction(loadedHTML);
}, 'html');
In myFunction, I want to make some changes to the loaded html and eventually return it. I can't get .find() to work. Here is what that code looks like:
function myFunction( html ) {
var $html = $("<div>" + html + "</div>");
console.log( "$html.html() = " + $html.html()); // works!
$html.find("img", function() {
console.log("found an image"); // doesn't work :(
});
}
I am killing myself with something that is probably really simply. Let me know how I am dumb please...
I'm almost sure that you cannot use find in the way that you have.
Try something like:
var $foundImages = $html.find("img");
console.log($foundImages.length);
Which would, in theory, output the number of images that were found.
The find method doesn't have a second parameter:
http://api.jquery.com/find/
You should try this:
function myFunction( html ) {
var $html = $("<div>" + html + "</div>");
console.log( "$html.html() = " + $html.html()); // works!
console.log($html.find("img"));
}
Simply assign id to your div tag .
like below,
var $html = $("<div id='placeholder'>" + html + "</div>");
and find img with it like below,
$("#placeholder").find("img", function() {
console.log("found an image"); // doesn't work :(
});
your resultant code,
function myFunction( html ) {
var $html = $("<div id='placeholder'>" + html + "</div>");
console.log( "$html.html() = " + $html.html()); // works!
$("#placeholder").find("img", function() {
console.log("found an image"); // doesn't work :(
});
}
.find() didn't have callback function in jquery. it have parameter for selectors,elements,jqueryObject only.you have to check with length or condition like this
if($html.find("img").length > 0){
// do stuff here
}
or
if($html.has("img")){
// do stuff here
}
You can use this .filter():
var found = $html.find("img").filter(function() {
return this;
});
console.log(found);
or make an array out of it with .map():
var found = $html.find("img").map(function() {
return this;
}).get(); // use get() method here to get the values in array
console.log(found.length); // would give you the length of array created.
jQuery.find() doesn't have a callback but you can extend jQuery to do what you want:
jQuery.fn.extend({
findEach: function (selector, callback) {
var found = this.find(selector);
if (typeof callback == 'function' && found.length > 0) {
found.each(callback);
}
return found;
}
});
Then use like you expect:
$html.findEach("img", function(key, value) {//will run for each image
console.log(key);
console.log(value);
console.log(this);
});

Pass html object through string into function

What I have is kinda unusual I guess. I have this function deleteItem which is triggered onclick and has the following parameters
function dItem(type,id,element,confirmed){
if(confirmed){
handle delete function
}else{
var c = ',';
popup('Are you sure you want to delete this item?',
{
"Yes":"dItem('"+type+"'"+c+id+c+element+c+true+")",
"Cancel":"popupClose()"
}
)
}
}
.. onclick='dItem("comment",15,this,false)' ..
In popup()'s second parameter are passed the buttons that are to be displayed in the popup and the functions they call respectively. The problem is that element is a HTMLDIV element and I cannot figure out a neat way to pass that through a string. The only solution I could come to think of is to have a global variable holding the element in question and not passing it at all, although I don't really want to do that since it's more of a hack rather than a solution. Does anybody have any idea how I can pass that element through a string? Thanks in advance!
EDIT:
This is how the buttons object b is being processed and turned into HTML. Do you see how I can supply it with an actual function instead of just a name in the form of string?
var _b = '';
for(var i in b){
_b+="<div onclick='"+b[i]+"'>"+i+"</div>";
}
It's more common to handle this situation with callbacks. You will need to alter your popup function for that to work.
Example:
popup('Are you sure you want to delete this item?', {
"Yes": function () {
dItem(type, id, element, confirmed);
},
"Cancel": function () {
popupClose();
}
});
As a workaround you could simply generate an unique ID for the element and use that to identify the element later on. Because your function is recursive you need to deal with the fact that element can be either a ELEMENT_NODE or a string.
for(var i in b){
var generatedId = i /* TODO: not sure this generates an unique id */;
_b += "<div id='" + generatedId + "' onclick='" + b[i] + "'>" + i + "</div>";
}
function dItem (type, id, element, confirmed) {
if (confirmed) {
// handle delete function
}else{
var elementId;
// find the elementId
if (element && element.nodeType && element.nodeType == 1) {
elementId = element.id;
}else if (typeof element == 'string') {
elementId = element
}else{
throw Error('Argument [element] is not a ELEMENT_NODE or string');
}
var args = [type, id, elementId, true];
popup('Are you sure you want to delete this item?', {
"Yes": "dItem(" + args.join(', ') + ")",
"Cancel": "popupClose()"
});
}
}

Categories