文章列表
 
您正在查看 "Javascript" 分类下的文章

2009-02-14 15:23
在ie下面,iframe背景默认为白色,如果要改变,最好的方法是在iframe的body中添加

<body style="backgroud-color:transparent;">

如果不能改动iframe的源码,但是iframe和主页面在同一主域下,可以在iframe的onload事件中去改变被引用的iframe的背景色。

function bgtransparent()
{
if (document.all)
{
    var frm = document.getElementById("loginstatus");
    var node = frm.Document.createElement('style');
    var cssText = 'body{background-color:transparent}';
    node.setAttribute('type', 'text/css');
    frm.Document.getElementsByTagName('head')[0].appendChild(node);
    if (node.styleSheet) node.styleSheet.cssText = cssText;
    else if (frm.Document.createTextNode) node.appendChild(frm.Document.createTextNode(cssText));
    if (frm.allowTransparency) frm.allowTransparency = true;
}
}

<iframe src="a.html" onload="bgtransparent();"></iframe>
 
2009-01-22 22:31
// ==UserScript==
// @name          jQuery
// @namespace     http://www.joanpiedra.com/jquery/greasemonkey
// @description    Play nicely with jQuery and Greasemonkey
// @author        Joan Piedra
// @homepage      http://www.joanpiedra.com/jquery/greasemonkey
// @include       http://myxingchenbian.cn/*
// ==/UserScript==


// Add jQuery
var GM_JQ = document.createElement('script');
GM_JQ.src = 'http://cache.soso.com/post/js/jquery.js';
GM_JQ.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(GM_JQ);

// Check if jQuery's loaded
function GM_wait() {
    if(typeof unsafeWindow.jQuery == 'undefined') { window.setTimeout(GM_wait,100); }
    else { $ = unsafeWindow.jQuery; letsJQuery(); }
}
GM_wait();

// All your GM code must be inside this function
function letsJQuery() {
try{
    if (window.location.href.match(/myxingchenbian.cn/))
    {
        $("div.cBody").eq(0).hide();
        $("div.tonglan").hide();
        $("div.right").hide();
        $("div.newsView").children().not($("#content")).each(function(){$(this).hide();});
        $("div.footer").hide();
        $("div.content_up").hide();
        $("div.content_end").hide();
        $("div.cupage").hide();
    }
}catch(e){alert (e);}
}


使用require特性,将js文件下载到本地,以后不必再下载

// ==UserScript==
// @name           hello jquery
// @namespace      http://jquery.com
// @description    using jquery to remove ads
// @include        http://www.myxingchenbian.cn/*
// @require           http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js
// @require           http://ajax.googleapis.com/ajax/libs/jqueryui/1.5.3/jquery-ui.min.js
// ==/UserScript==

try{
    if (window.location.href.match(/myxingchenbian.cn/))
    {
        $(".tonglan, .footer, span:hidden, .right, .ad_style, .info_a3, .cBody:first, body script").remove(); //去掉广告内容
        $("#content").children().each(function(i){if (i>0 && i<5) $(this).remove();});
        $(".thisPlace:first").prependTo("body").after($("#content")); //挪动需要的内容到body中
        if (window.location.href.match(/\d+/)) $(".main").remove(); //只有具体的页才把main去掉
        $("p:contains('myxingchenbian.cn'), p:contains(',,')").remove(); //去除夹杂的广告
        $("p").each(function(){if ($(this).text().length < 2) $(this).remove();}); //去除空的段落
        $("body").attr('style', 'font-size:16px'); //重置字体大小
    }
    else if (window.location.href.match(/lqqm.net/))
    {
        $("table[width='100%']:first td[valign='top']").remove();
    }
}catch(e){alert (e);}


安装

将上述文件保存为以".user.js"结尾命名的js文件,然后使用firefox打开,这个时候greasemonkey就会提示你进行安装,在安装的过程中,就会下载对应的js文件并保存到本地。
 
2008-09-26 23:01

function resize_iframe(iframe, tried, oldHeight)
{
try{
if (iframe.contentDocument) ibody = iframe.contentDocument.body;
else ibody = iframe.Document.body;
} catch(e) { iframe.style.height = '580px'; return;}
if (iframe.src.indexOf('/vote/') > 0)
{height = (ibody.offsetHeight > ibody.scrollHeight ? ibody.offsetHeight : ibody.scrollHeight);iframe.style.scrolling='no';}
else{height = (ibody.offsetHeight > ibody.scrollHeight ? ibody.offsetHeight : ibody.scrollHeight) + 20;}
if (height < 137) height = 137;
else if (height >2048) height = 2048;
iframe.style.height = height + 'px';
if (typeof(tried) == 'undefined') tried = 0;
++tried;
if (height != oldHeight && tried<5)
{
if (typeof(oldHeight) == 'undefined') oldHeight = 137;
else oldHeight = height;
setTimeout(function(){resize_iframe(iframe, tried, oldHeight)}, 100);
}
}

<iframe onload="resize_iframe(this);" marginheight="0" marginwidth="0" frameborder="0" scrolling="auto" width="700" height="137" src="{iframe_url|escape:"html"}"></iframe>

 
2008-09-26 12:18
function var_dump(obj) {
if(typeof obj== "object") {
var txt = '';
for(key in obj)
{
txt +=key + '=' + obj[key] + ',';
}
     return "Type: "+typeof(obj)+((obj.constructor) ? "\nConstructor: "+obj.constructor : "")+"\nValue: " + txt;
} else {
     return "Type: "+typeof(obj)+"\nValue: "+obj;
}
}
function resize_iframe(iframe)
{
ibody = iframe.contentWindow.document.body;
clipboardData.setData("text", var_dump(ibody));
}
 
2007-11-22 12:07

刚接触javascript,对于脚本放在html源码中的不同位置,执行顺序不太了解,做了个实验,贴出结果,记录下来以后用得着。

测试代码,如下:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
    <title>测试js执行顺序</title>
    <script language="JavaScript" type="text/javascript" defer>
      alert('defer里的内容')
    </script>
    <script language = 'javascript' type='text/javascript'>
      alert('head中普通的script');
      //document.onload = alert('onload里的script');
    </script>
    <script type="text/javascript" src="jquery-1.2.1.min.js"></script>
    <script type="text/javascript">$(function(){
        alert('jquery中的script');
      });
    </script>
</head>
<body onload="alert('写在body的onload事件中的script');">
    #############
    #############
    <br>
    <script language = 'javascript' type='text/javascript'>
      alert('页面中的script');
    </script>
    ◎◎◎◎◎◎◎◎◎◎◎◎◎
    ◎◎◎◎◎◎◎◎◎◎◎◎◎
</body>
</html>
<script language = 'javascript' type='text/javascript'>
alert('html闭合标签外的script');
</script>

比较这段代码在ie7和firefox中的表现

左侧是ie7弹出的现实列表,而右侧是firefox弹出的事件列表

对比可以发现,二者就是对defer的处理不同,其他还是一致的。什么是defer呢?这里是从网上找到的一段文字:

----------------------------------------

javascript权威指南 版4中说到:
使用src性质从外部javascript文件读取脚本时,HTML解析器和javascript解释器都会停止下来等待文件装载完毕。

浏览器之所以这样做(在下载js文件期间,停止一切html的解析和渲染), 是因为在文件下载回来之前无法知道 js 文件中是否会包含 document.write 这种改变 dom 的语句。

使用 defer 来告诉浏览器,
"我的这段js代码不会包含document.write 啦, 你先放心解析和显示其他html啊。
不用傻傻的等我,等你把html都处理完,再来处理我这里把"

这个时候浏览器就会暂时跳过你的这段js(就好像他们原本就不存在一样),等他忙完其他那些
乱七八糟的事情后才回过头来处理你的这个js,处理完你的这个js(就是包含defer属性的那些)后,

浏览器想:“应该有onload事件”,于是 window.onload事件触发了。

但是 firefox不支持 defer
----------------------------------------

所以才表现上述不同。这里我们可以看出:
1. ie才支持defer,所以如果写跨浏览器代码,就不要用它了
2. 在head之中的js脚本最先执行,如果这里面的js使用src来载入,需要等这部分js完全载入在能进行下一步动作。如果这部分代码不改变dom,最好还是放到页尾
3. 页面中的js代码早于jquery和onload事件执行
4. jquery的ready时间早于onload事件执行,它不必等待页面中的图片加载就可以执行
5. onload等待所有的页面内容(包括图片)加载完毕后执行

这里测试并不全面,还可以完善,有兴趣可以做一下

 
 
   
 
 
文章分类
 
   
 
文章存档
 
     
 
最新文章评论
  

debug hacks(中文版)里166页有一段脚本就是用来设置一些调试操作的。 脚本写在debu
 

tags.sh: line 22: cscope: command not found 这个是什么情况?
 

回复chy:我记得reverse.put.as 的作者写了个.gdbinit的脚本 很强大 你可以参考一
 

谢谢!写的不错,很受益。
 

大哥,应该加工已下呀,你这东西连主函数也没有,怎么看呀
   
帮助中心 | 空间客服 | 投诉中心 | 空间协议
©2012 Baidu