页面加载后如何从Node.js服务器向客户端发送其他JavaScript代码 [英] How to send additional javascript code to client from nodejs server after page load

查看:82
本文介绍了页面加载后如何从Node.js服务器向客户端发送其他JavaScript代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当弹出窗口显示在我的网站上时,我希望该弹出窗口中的div根据特定选择的设置呈现多种不同方式中的一种. 理想情况下,我希望按下一个按钮以打开弹出窗口,让客户端根据当前设置是什么,要求服务器提供所需的代码,然后一旦从服务器获取代码,便执行该代码并呈现该内容.弹出窗口.

When a popup shows up on my website, I want the div in that popup rendered one of many different ways depending on what a certain chosen setting is. Ideally, I'd want a button pressed to open the popup, have the client ask the server for the code it needs based on what the current setting is, then once it gets the code from the server execute it and render the contents of the popup.

我是否需要服务器上的代码,是否将功能另存为对象上的属性,然后将对象发送给客户端?不确定某些功能是否会因为与DOM相关的功能无法在服务器上编译而引发错误?

Do I require the code on the server, save the functions as properties on an object, and send the object to the client? Not sure if some of the functions would throw an error because DOM-related functions won't compile on a server?

我是否以文件形式发送功能,并让客户端使用诸如require.js或browserify之类的库在客户端上要求它?

Do I send the functions as a file and have the client require it on the client-side using a library like require.js or browserify?

推荐答案

我会使用 解决您的问题.

I would use eval() to solve your problem.

$.ajax({
    url:'MyServer/code.json',
    success : function(data, code) {
        eval(data.code);
    }
});

MyServer/code.json

{
    "code":"alert('Code from server executed.'); $('body').css({ 'background-color': '#000'});"
}

这是一个使用JSON的简单示例,这一点也不安全.请小心使用eval().

This is a simple example with JSON and this is not secure at all. Be carefull using eval().

这篇关于页面加载后如何从Node.js服务器向客户端发送其他JavaScript代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆