首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过DOM向页面添加多行javascript

问通过DOM向页面添加多行javascript
EN

Stack Overflow用户
提问于 2016-12-15 13:39:57
回答 1查看 182关注 0票数 0

我正在修补一些东西,更多的只是实验和学习DOM操作是如何工作的。

代码语言:javascript
复制
    <script type="text/javascript">
(function()
{
        var config = {
        };
        var get = CKFinder.tools.getUrlParam;
        var getBool = function( v )
        {
            var t = get( v );

            if ( t === null )
                return null;

            return t == '0' ? false : true;
        };

        var tmp;
        if ( tmp = get( 'basePath' ) )
            CKFINDER.basePath = tmp;

        if ( tmp = get( 'startupPath' ) )
            config.startupPath = decodeURIComponent( tmp );

        config.id = get( 'id' ) || '';

        if ( ( tmp = getBool( 'rlf' ) ) !== null )
            config.rememberLastFolder = tmp;

        if ( ( tmp = getBool( 'dts' ) ) !== null )
            config.disableThumbnailSelection = tmp;

        if ( tmp = get( 'data' ) )
            config.selectActionData = tmp;

        if ( tmp = get( 'tdata' ) )
            config.selectThumbnailActionData = tmp;

        if ( tmp = get( 'type' ) )
            config.resourceType = tmp;

        if ( tmp = get( 'skin' ) )
            config.skin = tmp;

        if ( tmp = get( 'langCode' ) )
            config.language = tmp;

        // Try to get desired "File Select" action from the URL.
        var action;
        if ( tmp = get( 'CKEditor' ) )
        {
            if ( tmp.length )
                action = 'ckeditor';
        }
        if ( !action )
            action = get( 'action' );

        var parentWindow = ( window.parent == window )
            ? window.opener : window.parent;

        switch ( action )
        {
            case 'js':
                var actionFunction = get( 'func' );
                if ( actionFunction && actionFunction.length > 0 )
                    config.selectActionFunction = parentWindow[ actionFunction ];

                actionFunction = get( 'thumbFunc' );
                if ( actionFunction && actionFunction.length > 0 )
                    config.selectThumbnailActionFunction = parentWindow[ actionFunction ];
                break ;

            case 'ckeditor':
                var funcNum = get( 'CKEditorFuncNum' );
                if ( parentWindow['CKEDITOR'] )
                {
                    config.selectActionFunction = function( fileUrl, data )
                    {
                        parentWindow['CKEDITOR'].tools.callFunction( funcNum, fileUrl, data );
                    };

                    config.selectThumbnailActionFunction = config.selectActionFunction;
                }
                break;

            default:
                if ( parentWindow && parentWindow['FCK'] && parentWindow['SetUrl'] )
                {
                    action = 'fckeditor' ;
                    config.selectActionFunction = parentWindow['SetUrl'];

                    if ( !config.disableThumbnailSelection )
                        config.selectThumbnailActionFunction = parentWindow['SetUrl'];
                }
                else
                    action = null ;
        }

        config.action = action;

        // Always use 100% width and height when nested using this middle page.
        config.width = config.height = '100%';

        var ckfinder = new CKFinder( config );
        ckfinder.replace( 'ckfinder', config );
})();
    </script>

我想知道怎么做,就是通过DOM将代码添加到页面。我知道如果我把这个函数放在>JS文件中,我可以做到,因为我知道如何调用.JS文件并正确加载它们。

如果我想把脚本直接写在页面上,我该怎么做呢?我找到了一些简单的例子,比如:

代码语言:javascript
复制
  window.onload = function() {
    var s = document.createElement('script');
    s.type = 'text/javascript';
    var code = 'alert("hello world!");';
    try {
      s.appendChild(document.createTextNode(code));
      document.body.appendChild(s);
    } catch (e) {
      s.text = code;
      document.body.appendChild(s);
    }
  } 

但是当我在里面放一个很长的、多行的javascript代码块时,它似乎不起作用。我该怎么做呢?

EN

回答 1

Stack Overflow用户

发布于 2016-12-15 14:02:58

DOM是一种基于标签的标记结构。因此,标签的已定义结构必须以已定义的顺序出现。DOM中的所有内容都放在标签中。如果您想在DOM中使用JavaScript,它也会被添加到脚本标记中。在这个标签中,你可以把你所有的JavaScript放在页面上。

下面的链接包含一个JavaScript JQuery示例,这是同一页上的代码,它是一篇非常初级的文章,详细介绍了如何在页面上使用脚本以及如何使用事件处理等。

JavaScript Basic Tutorial on JQuery and Event Handling

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41157155

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档