weixin_33711641 2012-12-13 23:59 采纳率: 0%
浏览 26

动态加载JavaScript

I am currently building an Ajax web application using JQuery that allows for users to have the same page open multiple times in the same area. By allowing this though it tends to cause conflicts with regards to the DOM and "unique ids".

As a result I have created a system where the javascript is preprogrammed if you like in PHP with a unique id before being send out. It's then sent to the user as a .php file.

The code is similar to this: User wishes to load a page in a content_area, that content area sends AJAX request to the server with the ID of that content_area which becomes the Unique Id. The server sends back a page like "myscript.php" which contains the following:

    <script>
$(function() {
    var unique_id="<?php echo $unique_id; ?>"

    var new_div=$("<div id='content" + unique_id + "'>");
    });
    </script>

I am wondering is this a bad idea and are there any alternatives to this?

  • 写回答

2条回答 默认 最新

  • weixin_33682790 2012-12-14 00:06
    关注

    You'd want to add a php header so the browser knows it's a JavaScript file and not php.

    <?php
    header('Content-type: text/javascript');
    ?>
    <script>
    $(function() {
     var unique_id="<?php echo $unique_id; ?>"
     var new_div=$("<div id='content" + unique_id + "'>");
     });
    </script>
    

    Then you could use:

    <script type="text/javascript" src="myscript.php">
    
    评论

报告相同问题?

悬赏问题

  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?