我有一个我写的javascript函数,它被用来包含一个外部JS文件,但只有一次.我需要这样一个函数的原因是因为在通过AJAX加载某些内容时调用它并且我需要为该内容运行特定于页面的代码(不,只是使用.live不会覆盖它).
这是我的尝试,为简洁而缩短:
$.include_once = function(filename) {
if ($("script[src='" + filename + "']").length === 0) {
var $node = $("<script></script>")
.attr({
src : filename,
type : "text/javascript"
})
;
$(document.body).append($node);
}
};
Run Code Online (Sandbox Code Playgroud)
这样可以正常工作:调用该函数,它加载外部文件,并在加载时运行该文件.完善.
问题是它将始终重新加载该外部文件:我用来检查脚本是否存在的查询总是找不到任何东西!
在调试时,我添加了一些行:
alert($("script").length); // alerts: 4
$(document.body).append($node);
alert($("script").length); // alerts: 4
Run Code Online (Sandbox Code Playgroud)
查看动态源代码(Firebug的HTML选项卡),我根本找不到脚本标记.
我知道我可以维护一个我以前包含的文件数组,但是我希望使用这样的方法,如果它工作的话,它看起来更健壮了,因为不是所有的JS文件都是以这种方式包括在内.
任何人都可以解释在第二个片段中看到的行为吗?
我有一个JSON文件的外部URL,该文件托管在另一个域(不是我的)上.是否可以仅使用javascript解析此信息?以下是JSON数据的示例.我只想得到"q"价值.
[{"url":"http://website.com/?q=who+is+ip+search","q":"who is ip search"},{"url":"http://website.com/?q=eclipse+visual+editor","q":"eclipse visual editor"},{"url":"http://website.com/?q=partition+recovery","q":"partition recovery"},{"url":"http://www.website.com/?q=katzenfurz","q":"katzenfurz"},{"url":"http://website.com/?q=rtfm","q":"rtfm"},{"url":"http://website.com/?q=Google+ist+Dein+Freund","q":"Google ist Dein Freund"}]
Run Code Online (Sandbox Code Playgroud) 在IE中,我可以像:
var x = document.getElementById("header");
alert(x.all[0].tagName);
Run Code Online (Sandbox Code Playgroud)
如果我在Firefox中尝试,我会收到错误"all is undefined".
Firefox的.all财产是什么?
我无法弄清楚如何创建一个会破坏php会话的javascript函数.我有一个可点击的链接,它将调用一个名为destroyphpsess的函数.我认为到目前为止这是可以的.现在我需要定义javascript函数.这是我到目前为止的代码......
if ($_SESSION['color'] == "") {
$var = "<a href='JavaScript:newPopup(\"http://www.yourfantasyfootballreality.com/register.php\");' class='two'>Register</a> | <a href='JavaScript:newPopup(\"http://www.yourfantasyfootballreality.com/signin.php\");' class='two'>Sign In</a>";
} else {
$var = "Hello, ".$_SESSION['color'] ."! | " . "<a href=\"http://www.yourfantasyfootballreality.com/index.php\" onclick=\"destroyphpsess()\" class='two'>Log Out</a>";
}
echo $var;
Run Code Online (Sandbox Code Playgroud)
现在我需要定义javascript函数.这是我遇到麻烦的地方.这是我到目前为止的基本概要......
function destroyphpsess()
{
<?php
session_destroy();
?>
}
Run Code Online (Sandbox Code Playgroud)
如果有人能帮助我完成这项功能,我将不胜感激!谢谢.
我扩展了Array原型:
if(typeof Array.prototype.filter === 'undefined') Array.prototype.filter = function(fun /*, thisp*/){
var len = this.length;
if(typeof fun != "function") throw new TypeError();
var res = [], thisp = arguments[1];
for(var i=0;i<len;i++){
if(i in this){
var val = this[i]; // in case fun mutates this
if(fun.call(thisp, val, i, this)) res.push(val);
}
}
return res;
};
Run Code Online (Sandbox Code Playgroud)
例如,我创建了数组:
var A = [ 1, 2, 3, 4, 5 ];
Run Code Online (Sandbox Code Playgroud)
然后我添加了额外的属性,我将使用:
A.creator = 'Rustam';
A.created = new Date();
Run Code Online (Sandbox Code Playgroud)
如果我将使用for-in循环,并且浏览器没有内置支持Array.filter,它将通过A.filter …
我有一个文本框供用户输入正整数.用户多次输入大量条目.加班,文本框包含以前条目的列表,当您开始键入类似于上一个条目的内容时.有时,用户不小心选择了以前的条目.我知道这是浏览器的一个功能.
反正有没有从javascript或jQuery清除这些以前的条目?
所以,我正在考虑使用文本框(密码模式),然后用Javascript或jQuery取消它.我认为如果textbox用于密码,浏览器将不会保留任何内容.
如果这是解决此问题的正确方法,如何使用Javascript或jQuery取消屏蔽文本框密码?
我正在做的是清除我在ASPX页面上的文本框的历史记录.
谢谢.
我不知道如何解释我面临的问题.我做了一个小片段.请检查以下内容:
var Package = {};
Object.defineProperty(Object.prototype, 'inherit',
{
value: function(Parent, args)
{
var temp = function(){};
temp.prototype = Parent.prototype;
this.prototype = new temp();
this.uber = Parent.prototype;
this.prototype.constructor = this;
},
enumerable: false
});
var Module1 = Package.Module1 = function() {
// code;
};
Module1.prototype.method1 = function() {
};
var Module2 = Package.Module2 = function() {
// code;
};
Module2.prototype.method2 = function() {
};
var Module3 = Package.Module3 = function() {
// code;
};
// using custom : object.prototype.inherit
// Module3 …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用socket.io和express进行示例客户端连接(具体来说,这个:http://www.catonmat.net/blog/nodejs-modules-socketio/,其中一些来自https的其他示例)://github.com/LearnBoost/socket.io/blob/master/examples/chat/app.js).
我已经完成了大部分设置和工作 - 服务器运行,我可以点击/获取index.jade文件到浏览器.但是,一旦发生这种情况,它什么都不做.
服务器:
var io = require('socket.io');
var express = require('express')
, stylus = require('stylus')
, nib = require('nib');
var app = express.createServer()
var io = io.listen(app);
app.configure(function () {
app.use(stylus.middleware({ src: __dirname + '/public', compile: compile }));
app.use(express.static(__dirname + '/public'));
app.set('views', __dirname);
app.set('view engine', 'jade');
function compile (str, path) {
return stylus(str)
.set('filename', path)
.use(nib());
};
});
app.get('/', function (req, res) {
res.render('index', { layout: false });
});
app.listen(8080); …Run Code Online (Sandbox Code Playgroud)