SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    peculiar problem

    Hi,
    can someone explain why this happens

    I have two files . trial.html and trial. js
    --------------------------------------------------
    trial.js


    alert("If this executes trial.js is loaded");
    function my_function()
    {
    ("hello world");
    }

    ---------------------------------------------------
    trial.html

    .
    .
    .
    <script src="trail.js">
    // alert() WORKS
    my_function(); // DOES NOT WORK... the control does not go inside a fucntion

    // the file is definitely loaded as the statment alert(); before the function defn executes

    </script>



    why does this happen. now if i tweak t a bit and put my_function() call in the js file

    i.e

    trial.js


    alert("If this executes trial.js is loaded");
    function my_function()
    {
    ("hello world");
    }
    my_function(); //THIS WORKS ONCE REMOVED FROM the html file
    ------------------------------------------------------------------------------------
    why does this happen

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,191
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Could you please post the actual source of the HTML and JS file as what you have above is quite confusing but i think i issue comes down to your function call not been wrapped in <script> tags. Also in your example code above you have what looks like an alert but the keyword is missing which would cause an error.

    Code:
    function my_function() {
        ("hello world");       // Won't work
        alert("hello world"); // Will work
    }

  3. #3
    SitePoint Member Serj's Avatar
    Join Date
    Nov 2011
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to have 2 separate Script tags like this:

    Code:
    <script src="trail.js" type='text/javascript'> </script>
    <script type='text/javascript'>
    
    // alert() WORKS
    my_function(); // DOES NOT WORK... the control does not go inside a fucntion 
    
    // the file is definitely loaded as the statment alert(); before the function defn executes
    </script>
    When you think you are calling the function, the browser actually ignores that and just loads the stuff in your file...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •