JS pass over the rear end of a code executed after the front end to get

Today received a demand, it is actually the back end to give me a pass over JS code and then the front to perform.

Because the back-end pass over the JS code is a string, so just getting a little muddled, previously never done such a thing just started so there is no way.

This project is the use of separate front and rear ends of the format, so it is impossible to render the code on the server, so can only think of ways in the browser.

Then dive to the Internet several methods,

First, the use Function method, the code is as follows

 

was str = 'alert (1)';
(New Function (str)) ();

Second, using the method eval

 

was str = 'alert (1)';
eval (str)

Third, if your project is a back-end rendering pages can be connected to the <script> </ script> in rendered, so there is no need to deal with the front-end.

 

These three methods can be used, mainly to see your project needs to decide what to use.

Guess you like

Origin www.cnblogs.com/gendan5/p/11447959.html