简体   繁体   English

通过URL在Django Page上执行JavaScript,而无需重新加载

[英]Execute JavaScript on Django Page through URL without reload

i have a webpage that loads certain JavaScript packages. 我有一个网页,可加载某些JavaScript程序包。

www.mySite.com

If i enter JavaScript commands in the browser console, i am able to interact with them. 如果我在浏览器控制台中输入JavaScript命令,则可以与它们进行交互。

Lets take 让我们来

alert('5')

as a simple example. 作为一个简单的例子。

I would like the same JavaScript calls to be executed without the browser console but through a specific URL like: 我希望在没有浏览器控制台的情况下通过相同的URL执行相同的JavaScript调用,例如:

www.mySite.com/?value=5

so that this leads to an execution of my JavaScript commands? 从而导致执行我的JavaScript命令?

Without the Page beeing reloaded/refreshed but staying in the actual state. 没有重新加载/刷新页面蜂,而是保持在实际状态。

My approach was to catch the extended URL in my Django View and execute the JavaScript command. 我的方法是在Django视图中捕获扩展URL,然后执行JavaScript命令。

View: 视图:

class ShowPage(View):
    def get(self, request, caseId):

        value = request.GET.get('value', '')

        if(value is not ''):
            // execute JavaScript here...
            return HttpResponse("<script>alert(" + value + ")</script>")
        else:
             ...
             return render(request, 'template.html', context)

But this leads to a loss of my page where i entered the URL. 但这会导致我输入URL的页面丢失。

Does anyone has an idea how to preserve the actual Browser content? 有谁知道如何保存实际的浏览器内容? So that it is possible to call the loaded Javascript packages? 这样就可以调用已加载的Javascript包了?

Another idea was to call JavaScript through Ajax. 另一个想法是通过Ajax调用JavaScript。 But how do i map a URL in Django to a Ajax request? 但是,如何将Django中的URL映射到Ajax请求?

Your current code is problematic on many levels, particularly because it allows the user to execute arbitrary JS on your page. 您当前的代码在很多层面上都是有问题的,尤其是因为它允许用户在您的页面上执行任意JS。 This is called a Cross-site Scripting (XSS) attack . 这称为跨站点脚本(XSS)攻击

Another issue is that you seem to want to add a GET parameter without "changing the state" of the page. 另一个问题是您似乎想要添加GET参数而不“更改页面的状态”。 Here you must remember that a GET request is, by definition, a communication between the client and server. 在这里,您必须记住,按照定义,GET请求是客户端和服务器之间的通信。 You can artificially change what the URL looks like using JS, but you cannot submit a GET request on the same page without reloading it. 您可以使用JS人工更改URL的外观,但是您不能在不重新加载的情况下在同一页面上提交GET请求。

This is why you certainly want to use AJAX, which allows you to fetch the contents from another page and return them to the current page in the background. 这就是为什么您肯定要使用AJAX的原因,它允许您从另一个页面获取内容并将其返回到后台的当前页面。 Usually this is done by creating a view that returns a JsonResponse (in Django 1.7+) (see Creating a JSON response using Django and Python ). 通常,这是通过创建一个返回JsonResponse的视图(在Django 1.7+中)来完成的(请参阅使用Django和Python创建JSON响应 )。

A simplified example would be to encode a view that simply displays some text. 一个简化的示例是对仅显示一些文本的视图进行编码。 You could then retrieve this text using AJAX (I recommend the jQuery for this--it's simple and well-documented) and do whatever you want with it, including alert it. 然后,您可以使用AJAX检索此文本(我建议为此使用jQuery,它很简单且有据可查),并可以对其进行任何处理,包括alert

Try to use a '#': 尝试使用“#”:

www.mySite.com#command

This doesn't reload the site. 这不会重新加载该站点。

Maybe this helps too: 也许这也有帮助:

url: https://css-tricks.com/snippets/javascript/get-url-and-url-parts-in-javascript/ 网址: https//css-tricks.com/snippets/javascript/get-url-and-url-parts-in-javascript/

url: Get current URL in web browser 网址: 在网络浏览器中获取当前网址

'#': http://www.w3schools.com/html/html_links.asp '#': http : //www.w3schools.com/html/html_links.asp

EDIT: 编辑:

use 'eval()' to execute the input: 使用'eval()'执行输入:

http://www.w3schools.com/jsref/jsref_eval.asp http://www.w3schools.com/jsref/jsref_eval.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM