Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

JQUERY JTABLE


Reply


29 Posts   8312 Views

Avatar
MarcusDalgren

Community Member, 288 Posts

10 January 2012 at 6:32am

Am I understanding you correctly that writing it like this:
var $img = $('<img src=\"mysite/javascript/phone.jpg\" title=\"Edit phone numbers\" />');
Instead of the way it's written in your post gives you an error?

Also when writing code like that I'd really recommend you to switch to '' instead of "". When using "" every time the $ sign pops up PHP will think that's a variable and try for a variable replacement which probably isn't what you want. Start off by delimiting the whole customScript() in '' and change that line to:
var $img = $("<img src=\'mysite/javascript/phone.jpg\' title=\'Edit phone numbers\' />");

Finally, unless you really really have to, adding code through Requirements::customScript is really not recommended. You get problems like the ones you're having now and you're mixing PHP and js code which isn't a very good idea.

Avatar
sergieboy

Community Member, 33 Posts

10 January 2012 at 8:22am

Edited: 10/01/2012 8:26am

Correct.
Without escaping : you get a syntax error.
With escaping : you get a 'missing variable name' error:
var = $('<img src=\"mysite/javascript/phone.jpg\" title=\"Edit phone numbers\" />');
It's like the $img disappeared from the FireBug screen.

I tried any kind of switching "" '', doesn't help.

When I start off by delimiting the whole customScript() in '' , there are a dozen of other error occuring.

"Finally, unless you really really have to, adding code through Requirements::customScript is really not recommended. You get problems like the ones you're having now and you're mixing PHP and js code which isn't a very good idea." Are you suggesting that I should include the javascript in the template ?

Attached Files
Avatar
MarcusDalgren

Community Member, 288 Posts

10 January 2012 at 8:28am

Yes either include the js in the template directly or simply link it as a custom external script. Are you depending on any variables from PHP in that customScript() function?

Avatar
sergieboy

Community Member, 33 Posts

10 January 2012 at 8:34am

Not depending on any variables from PHP in that customScript() function.
Will try it by linking it as an external custom script.

Avatar
martimiz

Forum Moderator, 1097 Posts

10 January 2012 at 10:19am

Edited: 10/01/2012 10:20am

Just my 2 cts worth, if ever you want to use customScript again:

I try and avoid javascript variables beginning with a $.

Also there is an issue, where you use a jQuery selector $(...) between brackets, like in: function(){$('ul').css('color','#ffffff');} SilverStripe, seeing the {$ bit, will now consider it a PHP variable and throw an error. Adding a space the bracket and dollarsign will fix that.

Happened to me lots of times :-(