简体   繁体   English

在.js页面中包含Jquery脚本

[英]Include Jquery script in .js page

Sorry if title is not too clear but I think it's about right. 抱歉,标题不太清楚,但我认为这是对的。 NEhow, what I would like to do is a bit like (well is to a certain extent) building a widget with JQuery (pref), PHP & CSS. NEhow,我想做的有点像(在一定程度上)用JQuery(首选),PHP和CSS构建一个小部件。

What I would really like to happen is for a "member" of my site to simply paste 2 lines of code in their HTML to load the widget. 我真正想做的是让我网站的“成员”只需在HTML中粘贴两行代码来加载小部件。 Something like this: 像这样:

<script type="text/javascript" src="http://www.mydomain.com/script.js"></script>

Then to display the widget something like this <div id="displaywidget"></div> 然后显示小部件,例如<div id="displaywidget"></div>

OK that bit is "easy" and ok. 好的,那一点是“容易的”,好的。 But how do I include JQuery or "something" to generate the widget in script.js 但是我如何包含JQuery或“ something”以在script.js中生成小部件

What I mean is "displaywidget" - the ID of the widget div will be the name of a php file on my server so essentially script.js will need to load displaywidget.php into the div displaywidget. 我的意思是“ displaywidget”-小部件div的ID将是服务器上php文件的名称,因此本质上script.js将需要将displaywidget.php加载到div displaywidget中。

I think I use document.getElementById('displaywidget') to get the div but how do I then "write/insert/load" displaywidget.php inside the div? 我想我使用document.getElementById('displaywidget')来获取div,但是如何在div内“写入/插入/加载” displaywidget.php?

Thinking as I write "pure" java can do "most of what I want ie document.getElementById('displaywidget') , BUT I would prefer to also "include" Jquery.js as I would like some aspects of the widget to use JQuery. Example being the JQuery UI date function. 在我编写“纯” java时可以想到,“我可以做大部分我想做的事,即document.getElementById('displaywidget') ,但是我更希望也“包含” Jquery.js,因为我希望小部件的某些方面使用JQuery 。示例是JQuery UI日期函数。

Sorry if I am rambling a bit but trying to think as I go along. 抱歉,如果我闲逛了一点,但尝试着想一想。 My "real" problem is I am not too sure on "pure" javascript ie getting the div to display/load displaywidget.php 我的“真正”问题是我不太确定“纯” javascript,即让div显示/加载displaywidget.php

Suggestions please. 请提出建议。 (Oh if I am barking up the wrong tree please feel free to tell me - nicely:) ) (哦,如果我在错误的树上吠叫,请随时告诉我-很好:))

Thanks in advance 提前致谢

I think I use document.getElementById('displaywidget') to get the div but how do I then "write/insert/load" displaywidget.php inside the div? 我想我使用document.getElementById('displaywidget')来获取div,但是如何在div内“写入/插入/加载” displaywidget.php?

You're looking for the AJAX behaviors inside of jQuery which would make the call to the php page and then push the data into the div. 您正在寻找jQuery内部的AJAX行为,该行为会调用php页面,然后将数据推送到div中。

You should be loading jQuery early on in the process, right up front in your head element. 您应该在此过程的早期,就在您的head元素中加载jQuery。 Once its loaded it will be cached so no worries of its on every page. 加载后将被缓存,因此每个页面都无需担心。 No real overhead incurred. 没有实际的开销。

Once jQuery is installed you can call one of many AJAX functions related to obtaining data and popluation elements. 安装jQuery后,您可以调用与获取数据和填充元素相关的许多AJAX函数之一。 Theres $.load(), $.ajax(), and a few others that escape me unless I go and check out their docs section. 除非我去检查他们的文档部分,否则会有$ .load(),$。ajax()和其他一些使我逃脱的东西。

You can do all of this without jQuery, but its more code and you have to control for browser differences. 您可以在没有jQuery的情况下完成所有这些工作,但是它需要更多代码,并且您必须控制浏览器的差异。

You can load jquery into script.js, just copy and paste it after or before whatever javascript lives in script.js. 您可以将jquery加载到script.js中,只需将其复制并粘贴到script.js中包含的任何JavaScript之后或之前。

So if script.js is: 因此,如果script.js是:

//start of file
alert('ex');
//end of file

Make it: 做了:

//start of file
alert('ex')
Copy and pasted Jquery source
//end of file

After a bit more "trawling & thought" I found this code: 经过更多的“拖延思考”后,我找到了以下代码:

(function() {
// Localize jQuery variable
var jQuery;
/******** Load jQuery if not present *********/
if (window.jQuery === undefined || window.jQuery.fn.jquery !== '1.4.2') {
var script_tag = document.createElement('script');
script_tag.setAttribute("type","text/javascript");
script_tag.setAttribute("src","http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js");
script_tag.onload = scriptLoadHandler;
script_tag.onreadystatechange = function () { // Same thing but for IE
if (this.readyState == 'complete' || this.readyState == 'loaded') {
scriptLoadHandler();
}
};
// Try to find the head, otherwise default to the documentElement
(document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);
} else {
// The jQuery version on the window is the one we want to use
jQuery = window.jQuery;
main();
}
/******** Called once jQuery has loaded ******/
function scriptLoadHandler() {
// Restore $ and window.jQuery to their previous values and store the
// new jQuery in our local jQuery variable
jQuery = window.jQuery.noConflict(true);
// Call our main function
main(); 
}
/******** Our main function ********/
function main() { 
jQuery(document).ready(function($) { 
******* Load CSS *******/
var css_link = $("<link>", { 
rel: "stylesheet", 
type: "text/css", 
href: "style.css" 
});
css_link.appendTo('head');          

/******* Load HTML *******/
var jsonp_url = "http://al.smeuh.org/cgi-bin/webwidget_tutorial.py?callback=?";
$.getJSON(jsonp_url, function(data) {
$('#example-widget-container').html("This data comes from another server: " + data.html);
});
});
}
})(); // We call our anonymous function immediately

writtend by Alex Marandon and found here http://alexmarandon.com/articles/web_widget_jquery/ - works a treat, exactly what I wanted, including/installing JQuery into a .js file 由Alex Marandon撰写,并在此处找到http://alexmarandon.com/articles/web_widget_jquery/-完全可以满足我的要求,包括/将JQuery安装到.js文件中

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

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