[英]pass a variable from one javascript file to another javascript file
This is my small code snippet to send the request message to the server and get back the response and prints it. 这是我的小代码段,用于将请求消息发送到服务器,并获取响应并进行打印。 It's a chrome add-on but the thing is if any onClick operations will happen on the browser then it will grab the url and has to send to the another JavaScript file then this JavaScript file is responsible for sending url to the node.js server
这是一个Chrome插件,但问题是,如果在浏览器上发生任何onClick操作,则它将抓取该网址并必须发送到另一个JavaScript文件,然后此JavaScript文件负责将网址发送到node.js服务器
the url.js(which grabs the url on click) file is : url.js(在点击时获取网址)文件是:
$(document).click(function (e) {
if ($(e.target).closest('a').length) {
// I want this url variable to be transferred from here to the below
// JavaScript file
var url = $(e.target).closest('a').attr('href');
} else {
alert('You did not click a link');
}
});
and the other javascript file which takes the parameter is : 另一个带有参数的javascript文件是:
window.WebSocket = window.WebSocket || window.MozWebSocket;
var connection = new WebSocket('ws://localhost:1337');
connection.onopen = function () {
connection.send(url);
connection.send("http://static.adzerk.net/Advertisers/bd294ce7ff4c43b6aad4aa4169fb819b.jpg");
};
connection.onerror = function (error) {
alert("something went wrong with your server " + error);
};
connection.onclose = function () {
alert("server had shut down now ");
};
connection.onmessage = function (message) {
try {
var json = JSON.parse(message.data);
} catch (e) {
console.log('This doesn\'t look like a valid JSON: ', message.data);
return;
}
if (json.type === 'message') {
addMessage(json.data.text);
} else {
console.log('Hmm..., I\'ve never seen JSON like this: ', json);
}
};
function addMessage(message) {
var rec_object = message;
var regexp1 = /[\/a-zA-Z]+(.jpg)/;
var regexp2 = /[\/a-zA-Z]+(.mp3)/;
var regexp3 = /[\/a-zA-Z]+(.mp4)/;
if (regexp1.test(rec_object)) {
document.write("<img width=\"300\" height=\"250\" src=\"" + rec_object + "\" \/>")
document.write("<br /><br /><br /><br /><br /><br /><br /><br />");
} else
if (regexp2.test(rec_object)) {
document.write("<audio controls>");
document.write("<source src=\"" + rec_object + "\"type=audio\/mpeg \/>");
document.write("</audio>");
document.write("<br /><br /><br /><br /><br /><br /><br /><br />");
} else
if (regexp3.test(rec_object)) {
document.write("<video width=\"300\" height=\"250\" controls>")
document.write("<source src=\"" + rec_object + "\"type=video\/mp4 \/>");
document.write("</video>");
} else
document.write(rec_object);
}
So how can I do it with out using query string? 那么,如何在不使用查询字符串的情况下做到这一点呢? I just want to send it from one file to another file.
我只想将其从一个文件发送到另一个文件。 I tried like above but I was not getting anything.
我像上面一样尝试过,但是什么也没得到。
In place of the following: 代替以下内容:
var url = $(e.target).closest('a').attr('href');
use a global variable: 使用全局变量:
window.url = $(e.target).closest('a').attr('href');
Then, in your other file also, use connection.send(window.url)
. 然后,在另一个文件中,也使用
connection.send(window.url)
。 This will get you going but read below. 这将带您前进,但请阅读以下内容。
Note:: Using global variables is a very bad practice. 注意:使用全局变量是非常不好的做法。 Ideally, I'd restructure your code to create a
Connection
object with a property named url
which is instantiated from within the callback and then you can simple call whatever methods you want to call on it. 理想情况下,我将对代码进行重组,以创建一个名为
url
的Connection
对象,该对象从回调中实例化,然后您可以简单地调用任何要在其上调用的方法。 All its methods will work with the local variable url
. 它的所有方法都可以使用局部变量
url
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.