最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Linking javaScript file in PHP file - Stack Overflow

programmeradmin2浏览0评论

So I have a PHP file on my server which handles callbacks from external sites. There is no HTML/CSS files, just the PHP file.

Now I have to run some javaScript Code inside the PHP file but the javaScript Code is dependent on an external js file which I have access to both by file and url.

How can I link this external file to my PHP so I can use it. I've tried a lot of things but none of those seem to work.

For simplicity here's the dummy code:

<?php
echo '<script type="text/javascript">

        Parse.initialize("ID1", "ID2");");

        var TestObject = Parse.Object.extend("Class");
        var testObject = new TestObject();
        testObject.save({Text: "Hi"}, {
            success: function(object) {
                alert("done");
            },
            error: function(model, error) {
                alert("error");
            }
        });
</script>';
?>

And it's dependent on parse.js which I have in my rootfolder and also can be accessed via .4.2.min.js

So I have a PHP file on my server which handles callbacks from external sites. There is no HTML/CSS files, just the PHP file.

Now I have to run some javaScript Code inside the PHP file but the javaScript Code is dependent on an external js file which I have access to both by file and url.

How can I link this external file to my PHP so I can use it. I've tried a lot of things but none of those seem to work.

For simplicity here's the dummy code:

<?php
echo '<script type="text/javascript">

        Parse.initialize("ID1", "ID2");");

        var TestObject = Parse.Object.extend("Class");
        var testObject = new TestObject();
        testObject.save({Text: "Hi"}, {
            success: function(object) {
                alert("done");
            },
            error: function(model, error) {
                alert("error");
            }
        });
</script>';
?>

And it's dependent on parse.js which I have in my rootfolder and also can be accessed via http://www.parsecdn./js/parse-1.4.2.min.js

Share Improve this question asked May 7, 2015 at 12:02 iitum studantiitum studant 8663 gold badges9 silver badges25 bronze badges 2
  • Do you even have <script type="text/javascript" src="/parse-1.4.2.min.js"></script> in your PHP ? – Coloco Commented May 7, 2015 at 12:06
  • 2 By the way you can close and reopen <?php ?> tags so I don't think you need to echo there – Coloco Commented May 7, 2015 at 12:07
Add a ment  | 

3 Answers 3

Reset to default 4

in the echo, before the tags, add:

<script src="*link to the script*"></script>

EDIT (You provided the link, so Ill do it for you):

<?php
echo '
         <script src='http://www.parsecdn./js/parse-1.4.2.min.js'></script>
        <script type="text/javascript">

        Parse.initialize("ID1", "ID2");");

        var TestObject = Parse.Object.extend("Class");
        var testObject = new TestObject();
        testObject.save({Text: "Hi"}, {
            success: function(object) {
                alert("done");
            },
            error: function(model, error) {
                alert("error");
            }
        });
</script>';
?>
<?php
    // PHP code goes here
?>

Include javascript external file :

<script src="path_to_js_script"></script>

Your JS code

        Parse.initialize("ID1", "ID2");");

        var TestObject = Parse.Object.extend("Class");
        var testObject = new TestObject();
        testObject.save({Text: "Hi"}, {
            success: function(object) {
                alert("done");
            },
            error: function(model, error) {
                alert("error");
            }
        });
</script>

You could load your .js include from within your javascript code. This answer might be of help: https://stackoverflow./a/950146/261350

发布评论

评论列表(0)

  1. 暂无评论