views:

48

answers:

2
chrome.tabs.executeScript(null, {code:"$.each(selectValues, function(key='" + timestamp + "', value='Custom')
{   
     $('#expire').
          append($(\"<option></option>\").
          attr(\"value\",key).
          text(value)); 
});"});

It says that the first line has a syntax error, and the WebKit inspector shows odd highlighting patterns.

What's wrong with that? A friend thinks I need to escape some characters somewhere.

Thanks for the help!

+2  A: 

That's definitely not valid syntax. You have an equal sign in the formal parameter list.

It will presumably end up as:

$.each(selectValues, function(key='someTime', value='Custom')
{   
     $('#expire').
          append($(\"<option></option>\").
          attr(\"value\",key).
          text(value)); 
});

I recommend you use the file option if possible, so you don't have to deal with escaping issues:

chrome.tabs.executeScript(null, {file: "myScript.js"});

The presumably the code in myScript.js will be something like:

$.each(selectValues, function(key, value)
{   
     $('#expire').
          append($("<option></option>").
          attr("value",key).
          text(value)); 
});

However, it's not clear what you're trying to do. jQuery.each takes a callback with two parameters, but what do you expect to happen to the values on the right of the equal sign?

Matthew Flaschen
I'm not sure how to set the variable to be used there, I've never used jquery. My question pertained to the code object that I am trying to execute, I'll deal with syntax errors with the jquery after. If you know the solution to either, that would help greatly!
Cyclone
What do you expect to happen with the values on the right hand side of the equal sign?
Matthew Flaschen
I've never used jquery before, I'm trying to pass them as the values to attr and text.
Cyclone
The two parameters to that function are provided by [each](http://api.jquery.com/jQuery.each/). They represent the index and value of the element to process.
Matthew Flaschen
After reading the docs, I think your solution will work. How do I pass the variables to the js file though? Are the variables universal within the plugin?
Cyclone
I think the the script running in `executeScript` only has access to the content page's variables.
Matthew Flaschen
So how can I pass it those values? I need to execute this javascript on the page, with the proper timestamp. One more issue is that it says that $ is undefined when the plugin calls it, yet when I input the exact same code in the console it works.
Cyclone
Ooh, would it work if I executed javascript on the page to set that to that?
Cyclone
+1  A: 

You cannot use each that way. $.each() expects a function as callback that expects two parameters: index and value.

jQuery.each( collection, callback(indexInArray, valueOfElement) )

You cannot pass your own data to a function definition.

This is not related to jQuery, but to JavaScript in general.

If you use each, the parameters that get passed to the function come from the collection. You cannot pass custom parameters.

What do you want to do? If you want to set #expire just do it without each:

$("<option></option>").val('Custom').text(timestamp).appendTo('#expire');

If you are unfamiliar with jQuery, then read a tutorial.


In other languages like PHP and Python it means that you provide a default value to the parameters. However this is not possible in JavaScript (in this way).

Felix Kling