如何使用书签加载 javascript 文件,而该书签又不会重定向/重写页面?
我正在尝试加载此远程调试解决方案来实时调试/与 DOM/js 交互。说明要求您将 script
块和 script
链接放入页面的 HEAD
中,以使其全部正常工作。没关系。这是一个很棒的工具。
但是,如果我想要一种类似书签的方式将其“插入”到任何页面中,该怎么办?我不明白为什么这不能完成。
这是我的书签:
javascript:(function(){document.body.appendChild(document.createElement('script')).src='http://url-to-js-file.js?x='+(Math.random());})();
然后“url-to-js-file.js”加载 iphonedebug 的 js :
ipdConfig = {
baseUrl: '/ipd/', // trailing slash!
consoleBaseUrl: 'http://192.168.1.25:8170/' // trailing slash!
}
document.body.appendChild(document.createElement('script')).src='http://192.168.1.25/ipd/ipd.js';
console.log('fin.');
请注意,src
属性最初是“/ipd/ipd.js”,如说明所示。但如果我想让调试器与其他站点一起工作,它就需要像这样绝对。
我的问题是这段代码会将第一个js文件(url-to-js-file.js)转储到页面中,但是当第一个文件尝试插入其他连续文件时,我的页面似乎重定向。事实上它并没有重定向,只是被重写了。 Firebug 将结果页面显示为没有正文,而只是 HEAD
元素中的 ipd js 文件。
为什么要这样做?
是否是因为 小书签行为 通常会重定向到新页面?我不这么认为。因为我在书签链接中使用了闭包。当我注释掉“url-to-js-file.js”文件的 document.body...
行时,页面保持在原来的位置。是筑巢吗?或者可能是其他 ipd js 文件中的某些内容导致了这种情况?
我尝试将“url-to-js-file.js”代码放入闭包中,但随后全局 var ipdConfig
对 ipd 代码的其余部分变得不可见,从而破坏了它。
有什么想法吗?
I'm trying to load this remote debugging solution to debug/interact with DOM/js live. The instructions ask you to put a script
block and script
link into the HEAD
of the page to get it all working. That's fine. It's a great tool.
But what if i wanted a bookmarklet-like way of just "inserting" it into any page. I don't see why this can't be done.
This is my bookmarklet:
javascript:(function(){document.body.appendChild(document.createElement('script')).src='http://url-to-js-file.js?x='+(Math.random());})();
And then "url-to-js-file.js" loads the js for iphonedebug:
ipdConfig = {
baseUrl: '/ipd/', // trailing slash!
consoleBaseUrl: 'http://192.168.1.25:8170/' // trailing slash!
}
document.body.appendChild(document.createElement('script')).src='http://192.168.1.25/ipd/ipd.js';
console.log('fin.');
Note that the src
property there originally was "/ipd/ipd.js" like the instructions suggest. But it will need to be absolute like this if i want to get the debugger working with other sites.
My problem is that this code will dump the first js file (url-to-js-file.js) into the page ok but when the first file tries to insert the other successive files my page appears to redirect. In fact it does not redirect but just get rewritten. Firebug shows the resulting page as no body but just the ipd js files in the HEAD
element.
Why does it do this?
Is it because of the bookmarklet behavior that normally redirects to a new page? I don't think so. Because i'm using a closure in the bookmarklet link. And when i comment out the document.body...
line of the "url-to-js-file.js" file the page stays where it is. Is it the nesting? Or may it be something in the other ipd js files causing this?
I've tried putting my "url-to-js-file.js" code into a closure but then the global var ipdConfig
becomes invisible to the rest of the ipd code which breaks it.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最好的猜测是 ipd.js 文件在其代码中的某处使用了 document.write() 。在这种情况下,您需要修改
ipd.js
以使其不会执行此操作。正如我所怀疑的,
ipd.js
是由 javascript 新手编写的:修改它以使用
appendChild(createElement(...))
代替。My best guess is that the
ipd.js
file is usingdocument.write()
somewhere in its code. In which case you need to modifyipd.js
so that it doesn't do this.As I've suspected,
ipd.js
is written by a javascript newbie:Modify it to use
appendChild(createElement(...))
instead.