`
asyuanbo
  • 浏览: 4299 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

jquery append执行script脚本

 
阅读更多

jquery append事件会执行内容的script脚本;

eg:

$('#test').append("<script>alert(123);</script>")

 查看Jquery源码:

function domManip( collection, args, callback, ignored ) {

	// Flatten any nested arrays
	args = concat.apply( [], args );

	var first, node, hasScripts,
		scripts, doc, fragment,
		i = 0,
		l = collection.length,
		iNoClone = l - 1,
		value = args[ 0 ],
		isFunction = jQuery.isFunction( value );

	// We can't cloneNode fragments that contain checked, in WebKit
	if ( isFunction ||
			( l > 1 && typeof value === "string" &&
				!support.checkClone && rchecked.test( value ) ) ) {
		return collection.each( function( index ) {
			var self = collection.eq( index );
			if ( isFunction ) {
				args[ 0 ] = value.call( this, index, self.html() );
			}
			domManip( self, args, callback, ignored );
		} );
	}

	if ( l ) {
		fragment = buildFragment( args, collection[ 0 ].ownerDocument, false, collection, ignored );
		first = fragment.firstChild;

		if ( fragment.childNodes.length === 1 ) {
			fragment = first;
		}

		// Require either new content or an interest in ignored elements to invoke the callback
		if ( first || ignored ) {
			scripts = jQuery.map( getAll( fragment, "script" ), disableScript );
			hasScripts = scripts.length;

			// Use the original fragment for the last item
			// instead of the first because it can end up
			// being emptied incorrectly in certain situations (#8070).
			for ( ; i < l; i++ ) {
				node = fragment;

				if ( i !== iNoClone ) {
					node = jQuery.clone( node, true, true );

					// Keep references to cloned scripts for later restoration
					if ( hasScripts ) {

						// Support: Android<4.1, PhantomJS<2
						// push.apply(_, arraylike) throws on ancient WebKit
						jQuery.merge( scripts, getAll( node, "script" ) );
					}
				}

				callback.call( collection[ i ], node, i );
			}

			if ( hasScripts ) {
				doc = scripts[ scripts.length - 1 ].ownerDocument;

				// Reenable scripts
				jQuery.map( scripts, restoreScript );

				// Evaluate executable scripts on first document insertion
				for ( i = 0; i < hasScripts; i++ ) {
					node = scripts[ i ];
					if ( rscriptType.test( node.type || "" ) &&
						!jQuery._data( node, "globalEval" ) &&
						jQuery.contains( doc, node ) ) {

						if ( node.src ) {

							// Optional AJAX dependency, but won't run scripts if not present
							if ( jQuery._evalUrl ) {
								jQuery._evalUrl( node.src );
							}
						} else {
							jQuery.globalEval(
								( node.text || node.textContent || node.innerHTML || "" )
									.replace( rcleanScript, "" )
							);
						}
					}
				}
			}

			// Fix #11809: Avoid leaking memory
			fragment = first = null;
		}
	}

	return collection;
}

 hasScripts帮你执行脚本。

如果不想执行(防xss注入之类):

1、转义<script>标签

2、过滤掉<script>

3、<script type='text'>

分享到:
评论

相关推荐

    jquery-1.1.3 效率提高800%

    选择器速度提升 选择器的速度大幅度提高了,下表为jQuery1.1.2和1.1.3的选择器速度对比,提高了8倍多 Browser jQuery 1.1.2 jQuery 1.1.3 % Improvement IE 6 4890ms 661ms 740% Firefox 2 5629ms 567...

    jquery及js实现动态加载js文件的方法

    如果用jquery append直接加载script标签的话,会报错的。除了[removed]外,还有没有其他的比较好的动态加载js文件的方法。 解决方法: 1、jquery方法 $.getScript("./test.js"); //加载js文件 $.getScript("./test....

    appendTemplate.js:一个 jQuery 插件,允许您在定义要添加到文件中的变量后将文件插入到 DOM 中

    厌倦了从脚本中附加大量 HTML 吗? 我是。 // Ew.$ ( ".sidebar" ) . append ( "&lt;div class='inner-wrapper'&gt;&lt;div class='heading'&gt;... script type =" text/javascript " src =" https://code.jquery.com/jquery-1.11.

    JSer开源脚本框架 v2.8 GBK

    修复append(content), html(html)等方法载入内容中包含的script代码无法执行的BUG; 增加静态方法JSer.exec&#40;path&#41;实现JS文件动态载入执行; 增加JSer对象方法children(sub), children()等; 增加Url对象...

    JSer开源脚本框架 v2.8 UTF-8

    修复append(content), html(html)等方法载入内容中包含的script代码无法执行的BUG; 增加静态方法JSer.exec&#40;path&#41;实现JS文件动态载入执行; 增加JSer对象方法children(sub), children()等; 增加Url对象...

    SignalR 2.0 and MVC 5即时通讯完整版示例

    在解决方案资源管理器中,查看正在运行的应用程序的脚本文档节点 如果您使用Internet Explorer作为浏览器,则此节点在调试模式下可见。有一个名为hubs的脚本文件,SignalR库在运行时动态生成。这个文件管理jQuery...

    JAVA上百实例源码以及开源项目源代码

    使用时候只需在控制台窗口执行jar就可以了。 Java 3DMenu 界面源码 5个目标文件 内容索引:Java源码,窗体界面,3DMenu  Java 3DMenu 界面源码,有人说用到游戏中不错,其实平时我信编写Java应用程序时候也能用到吧,...

    xheditor-1.1.14

    方法2:利用xhEditor提供的jQuery插件接口来实现对特定textarea的初始化和传递参数,例: $('#elm1').xheditor(); 或者 $('#elm1').xheditor({tools:'mini'}); 特别说明:两种初始化方法只能选择其中一种使用,如果...

    JAVA上百实例源码以及开源项目

    使用时候只需在控制台窗口执行jar就可以了。 Java 3DMenu 界面源码 5个目标文件 内容索引:Java源码,窗体界面,3DMenu  Java 3DMenu 界面源码,有人说用到游戏中不错,其实平时我信编写Java应用程序时候也能用到吧,...

Global site tag (gtag.js) - Google Analytics