外部Javascript文件无法从头部的javascript块接收变量

时间:2012-11-22 21:42:54

标签: php javascript

我一直在网上搜索几个小时,在整个“外部js文件”-jungle中找不到答案。我希望你们能帮忙!

简而言之:我的外部javascript文件似乎没有得到我在main.php文件中定义的变量..

  1. main.php 上我定义了php变量并将它们“转换”为 javascript变量

    <head>...
    <script type="text/javascript">
    var phpmain_img = <?php echo json_encode($main_img); ?>;
    var phpvar1_large = <?php echo json_encode($var1_large); ?>;
    var phpvar2_large = <?php echo json_encode($var2_large); ?>;
    var phpvar3_large = <?php echo json_encode($var3_large); ?>;
    var phpvar4_large = <?php echo json_encode($var4_large); ?>;
    </script>
    ...
    <script language="javascript" type="text/javascript" src="/wshop/ext.js"></script>
    </head>
    
  2. 在我的 ext.js 文件中,我想处理这些变量。在里面     ext.js文件我定义了将要使用的函数swapImage()     回到主PHP:

            var imgArray = new Array(
            phpmain_img,
            phpvar1_large,
            phpvar2_large,
            phpvar3_large
            );
    
        function swapImage(imgID) {
            var theImage = document.getElementById('theImage');
            var newImg;
            newImg = imgArray[imgID];
            theImage.src = newImg;
        }
    
        function preloadImages() {      
            for(var i = 0; i < imgArray.length; i++) {
                var tmpImg = new Image;
                tmpImg.src = imgArray[i];
            }
        }
    
  3. 结果: main.php 中的swapImage()无法正常工作

    <div id="image">
        <img id="theImage" src="<?=$main_img; ?>" alt="" />
    </div>
    
    <div id="thumbs">
    <?php
    
            echo "<img src=\"<$main_img_small\" alt=\"\" onmouseover=\"swapImage(0);\">";
            echo "<img src=\"$var1_small\" alt=\"\" onmouseover=\"swapImage(1);\">";
            echo "<img src=\"$var2_small\" alt=\"\" onmouseover=\"swapImage(2);\">";
            echo "<img src=\"$var3_small\" alt=\"\" onmouseover=\"swapImage(3);\">";
    
            ?>
    
    
        <br />
    </div>
    

  4. 非常感谢任何帮助!

    更新

    我没有收到特定错误,swapImage函数在mouseover时不起作用。但是,我试图用例如输出变量。 document.write(phpimg_main)但似乎没有任何东西让我相信交换变量有问题......

    这是源代码浏览器输出

    <html>
        <head>
            <link href="../demo.css" rel="stylesheet" type="text/css" />
            <style type="text/css">
                ....
    </style>
    
            <script type="text/javascript">
            var phpmain_img = {"0":"http:\/\/path\/to\/main\/image.jpg"};
            var phpvar1_large = {"0":"http:\/\/path\/to\/image1.jpg"};
            var phpvar2_large = {"0":"http:\/\/path\/to\/image2.jpg"};
            var phpvar3_large = null;
            var phpvar4_large = null;
            </script>   
    
            <script language="javascript" type="text/javascript" src="/wshop/ext.js"></script>
    </head>
    
    
    
    
    <body onload="preloadImages()">
    
        <div id="image">
            <img id="theImage" src="http://path-to-main-image.jpg" alt="" />
        </div>
    
        <div id="thumbs">
        <img src="http://path-to-main-image.jpg" alt="" onmouseover="swapImage(0);"><img src="http://path-to-image1.jpg" alt="" onmouseover="swapImage(1);"><img src="http://path-to-image2.jpg" alt="" onmouseover="swapImage(2);">
    
            <br />
        </div>
    
    
    
    </body>
    

    `

    更新2:

    感谢您的意见和建议!当然,你是对的,我需要一个字符串而不是一个对象,所以编码是一个很好的提示。

    然而问题仍然没有用[0]解决。即使我像下面那样硬编码,第二个javascript块(我之前尝试将其外包为外部js文件)也不会获得第一个javascript块中定义的变量。

    <script type="text/javascript">
            var phpmain_img = "http://www.abc.de/path-img_main.jpg";
            var phpvar1_large = "http://www.abc.de/path-img1.jpg";
            var phpvar2_large = "http://www.abc.de/path-img2.jpg";
            var phpvar3_large = "http://www.abc.de/path-img3.jpg";
            var phpvar4_large = "http://www.abc.de/path-img4.jpg";
            </script>
    
    <script language="javascript" type="text/javascript">
    
    
                       var imgArray = new Array(
                    phpmain_img,
                    phpvar1_large,
                    phpvar2_large,
                    phpvar3_large,
                    phpvar4_large
                );
    
    
    
                function swapImage(imgID) {
                    var theImage = document.getElementById('theImage');
                    var newImg;
                    newImg = imgArray[imgID];
                    theImage.src = newImg;
                }
    
                function preloadImages() {      
                    for(var i = 0; i < imgArray.length; i++) {
                        var tmpImg = new Image;
                        tmpImg.src = imgArray[i];
                    }
                }
    
    
    
                </script>
    

    浏览器来源视图输出:

    <script type="text/javascript">
            var phpmain_img = "http://www.abc.de/path-img_main.jpg";
            var phpvar1_large = "http://www.abc.de/path-img1.jpg";
            var phpvar2_large = "http://www.abc.de/path-img2.jpg";
            var phpvar3_large = "http://www.abc.de/path-img3.jpg";
            var phpvar4_large = "http://www.abc.de/path-img4.jpg";
            </script>
    
            <script language="javascript" type="text/javascript"> //this i actually wanted to outsource into an external js-file
    
            var imgArray = new Array(
                phpmain_img,
                phpvar1_large,
                phpvar2_large,
                phpvar3_large,
                phpvar4_large
            );
    
    
    
            function swapImage(imgID) {
                var theImage = document.getElementById('theImage');
                var newImg;
                newImg = imgArray[imgID];
                theImage.src = newImg;
            }
    
            function preloadImages() {      
                for(var i = 0; i < imgArray.length; i++) {
                    var tmpImg = new Image;
                    tmpImg.src = imgArray[i];
                }
            }
    
    
    
            </script>
    

    对不起,这里有一个长话题。我希望你能效仿!我正在尽力学习!

1 个答案:

答案 0 :(得分:1)

虽然我不是PHP专家,但你的意思是

var phpmain_img = "<?php echo urlencode($main_img); ?>";

而不是

var phpmain_img = <?php echo json_encode($main_img); ?>;

如果查看渲染标记,很明显图像变量不包含您的预期:

var phpmain_img = {"0":"http:\/\/path\/to\/main\/image.jpg"};

而不是字符串,它是一个对象。