在内容窗格中加载 html 中的 javascript
我有一个 html 文件,我想将其从各个页面加载到 dijit.contentpane 中。内容加载正常(我只是设置了内容窗格的 href),但问题是 href 指定的 html 文件中的 javascript 似乎没有在一致的时间执行。
最终目标是将 html 文件加载到文件锚点处的内容窗格中(即,如果您输入 index.html#tag 以便跳转到文件的特定部分)。我尝试了几种不同的方法,但似乎没有任何效果。
我尝试过的:
1. (参考dijit.contentpane的href)
href="page.htm#anchor"
2. (再次,参考 dijit.contentpane 的 href —— 并没有真正期望这能起作用,但还是决定尝试一下)
href="#anchor"
3.(最后一次尝试在 href 指定的 html 内)
<script type="text/javascript">
setTimeout("go_to_anchor();", 2000);
function go_to_anchor()
{
location.href = "#anchor";
}
</script>
这最后一次尝试是最接近的他们所有人的工作。 2秒后(我把延迟放在那里,看看dijit代码中的某些内容是否可能与我的javascript同时加载),我可以看到浏览器短暂跳转到html页面中的正确位置,但然后立即返回到页面顶部。
I have an html file that I want to be loaded from various pages into a dijit.contentpane. The content loads fine (I just set the href of the contentpane), but the problem is that javascript within the html file specified by href doesn't seem to be executed at a consistent time.
The final goal of this is to load an html file into a contentpane at an anchor point in the file (i.e. if you typed in index.html#tag in order to jump to a certain part of the file). I've tried a few different methods and can't seem to get anything to work.
What I've tried:
1.
(refering to the href of the dijit.contentpane)
href="page.htm#anchor"
2.
(again, refering to the href of the dijit.contentpane -- didn't really expect this to work, but decided to try anyways)
href="#anchor"
3. (with this last try inside the html specified by href)
<script type="text/javascript">
setTimeout("go_to_anchor();", 2000);
function go_to_anchor()
{
location.href = "#anchor";
}
</script>
This last try was the closest to working of all of them. After 2 seconds (I put the delay there to see if something in the dijit code was possibly loading at the same time as my javascript), I could see the browser briefly jump to the correct place in the html page, but then immediately go back to the top of the page.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Dojo 在 URL 中使用散列来允许为通过 ajax 调用加载的页面添加书签。
这是通过 dojo.hash API 完成的。
所以...我认为你能做的最好的事情就是使用它来触发你在主页中编写的回调。
为了滚动到加载内容中的给定位置,您可以使用node.scrollIntoView()。
例如,假设您有一个名为“mainPane”的 ContentPane 页面,其中加载了一个名为“fragment.html”的 html 片段,并且您的片段包含 2 个像这样的锚点:
-fragment.html :
现在假设您有 2 个按钮主页(名为 btn1 和 btn2),将用于加载片段并导航到正确的锚点。然后,您可以在主页中使用以下 javascript 将其连接起来:
Dojo uses hashes in the URL to allow bookmarking of pages loaded through ajax calls.
This is done through the dojo.hash api.
So... I think the best thing you can do is use it to trigger a callback that you write inside your main page.
For scrolling to a given position in your loaded contents, you can then use node.scrollIntoView().
For example, say you have a page with a ContentPane named "mainPane" in which you load an html fragment called "fragment.html", and your fragment contains 2 anchors like this :
-fragment.html :
Now say you have 2 buttons in the main page (named btn1 and btn2), which will be used to load your fragment and navigate to the proper anchor. You can then wire that up with the following javascript, in your main page :
如果您正在加载的内容包含 javascript,您应该使用
dojox.layout.ContentPane
。If the content you're loading contains javascript you should use
dojox.layout.ContentPane
.