简体   繁体   English

在内容窗格中的html中加载javascript

[英]Load javascript in html within content pane

I have an html file that I want to be loaded from various pages into a dijit.contentpane. 我有一个html文件,我希望将其从各个页面加载到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. 内容加载正常(我只是设置了contentpane的href),但是问题是href所指定的html文件中的javascript似乎并没有在一致的时间执行。

The final goal of this is to load an html file into a contentpane at an anchor point in the file (ie if you typed in index.html#tag in order to jump to a certain part of the file). 这样做的最终目标是将html文件加载到文件锚点中的内容窗格中(即,如果您键入index.html#tag以便跳至文件的特定部分)。 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) 1.(指的是dijit.contentpane的href)

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) 2.(再次,引用dijit.contentpane的href-并不是真的希望它起作用,但还是决定尝试尝试)

href="#anchor"

3. (with this last try inside the html specified by href) 3.(最后一次尝试在href指定的html内)

<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. 2秒后(我将延迟放在那里以查看dijit代码中的某些内容是否可能与我的javascript同时加载),我可以看到浏览器短暂跳转到html页面中的正确位置,但随后立即返回到页面顶部。

Dojo uses hashes in the URL to allow bookmarking of pages loaded through ajax calls. Dojo在URL中使用哈希值,以对通过ajax调用加载的页面添加书签。 This is done through the dojo.hash api. 这是通过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(). 为了滚动到已加载内容中的给定位置,可以使用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 : 例如,假设您有一个名为ContentPane的页面,名为“ mainPane”,在其中加载了一个名为“ fragment.html”的html片段,并且该片段包含2个这样的锚点:

-fragment.html : -fragment.html:

<a href="#anchor1">Anchor 1</a>
<p>some very long contents...</p>
<a href="#anchor2">Anchor 2</a>
<p>some very long contents...</p>

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. 现在,假设您在主页上有2个按钮(分别为btn1和btn2),这些按钮将用于加载片段并导航至适当的锚点。 You can then wire that up with the following javascript, in your main page : 然后,您可以在主页上使用以下javascript将其连接起来:

<script type="text/javascript">
        require(['dojo/on', 
                 'dojo/hash', 
                 'dojo/_base/connect', 
                 'dijit/layout/BorderContainer', 
                 'dijit/layout/ContentPane',
                 'dijit/form/Button'], 
                 function(on, hash, connect){
                     dojo.ready(function(){
                         var contentPane = dijit.byId('mainPane');
                         var btn1 = dijit.byId('btn1');
                         var btn2 = dijit.byId('btn2');
                         btn1.on("Click", function(e){
                              if (!(contentPane.get('href') == 'fragment.html')) {
                                  contentPane.set("href", "fragment.html");
                              }
                              hash("anchor1");
                         });

                         btn2.on("Click", function(e){
                              if (!(contentPane.get('href') == 'fragment.html')) {
                                  contentPane.set("href", "fragment.html");
                              }
                              hash("anchor2");
                         });

                         // In case we have a hash in the URL on the first page load, load the fragment so we can navigate to the anchor.
                         hash() && contentPane.set("href", "fragment.html");

                         // This callback is what will perform the actual scroll to the anchor
                        var callback = function(){
                            var anchor = Array.pop(dojo.query('a[href="#' + hash() + '"]'));
                            anchor && anchor.scrollIntoView();
                        };

                        contentPane.on("DownloadEnd", function(e){
                            console.debug("fragment loaded");
                            // Call the callback the first time the fragment loads then subscribe to hashchange topic
                            callback();
                            connect.subscribe("/dojo/hashchange", null, callback);
                        });
                    }); // dojo.ready
            }); // require              
</script>

如果您要加载的内容包含javascript,则应使用dojox.layout.ContentPane

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM