I want to pass in a jQuery command (in the form of a string) from server-side JS to client-side js. This allows me to modify client-side DOM stuff from the server-side.
Function:
$("textArea").attr("disabled","true");
What I want to do:
$['$("textArea").attr("disabled","true")']();
Throws an error. Thoughts?
-
1This allows me to modify client-side DOM stuff from the server-side. This is not the right way.Ram– Ram2013年04月15日 22:58:59 +00:00Commented Apr 15, 2013 at 22:58
-
1near duplicate of stackoverflow.com/questions/15936011/… but like undefined says, this just smells wrong...Alnitak– Alnitak2013年04月15日 22:59:13 +00:00Commented Apr 15, 2013 at 22:59
-
It's a javascript chatroom. Disabling of html / dom elements must be done client-side. Trying to prevent any chatroom guests from fiddling with their browser element inspector - which can allow them to make posts even if they're "muted"Shazboticus S Shazbot– Shazboticus S Shazbot2013年04月15日 23:03:37 +00:00Commented Apr 15, 2013 at 23:03
-
Not sure why people are downvoting this question. It's perfectly legit. @Alnitak that's not a comparable situation. I can't break up this string client-side without revealing its functionalityShazboticus S Shazbot– Shazboticus S Shazbot2013年04月15日 23:04:48 +00:00Commented Apr 15, 2013 at 23:04
-
1@ShazboticusSShazbot You can't rely on JS hacks to implement security. Users will always find a way around them.Alnitak– Alnitak2013年04月15日 23:04:50 +00:00Commented Apr 15, 2013 at 23:04
2 Answers 2
You could use the eval-function on the client-side:
This will execute your javascript immediately:
eval('$("textArea").attr("disabled","true")');
But, as said in the comments, be careful with what you do as this is a very crude method.
Also, in terms of security, you don't really gain anything, because one could still open the dev-tools and remove the disabled attribute
1 Comment
Alternatively, you could break your string up into multiple strings passed from the server. For example:
// variables passed from the server
selector = 'textArea';
method = 'attr';
arguments = ['disabled', 'true'];
Then you could evaluate it this way:
$(selector)[method](arguments[0], arguments[1]);
Of course, if the number of arguments needs to be dynamic it would get a bit trickier.