SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot nikola8's Avatar
    Join Date
    Jun 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Sending some var to php file (problem please help)

    I'm calling my remote php script with

    Code:
    <script language="JavaScript" src="myscript.php" type="text/javascript"></script>

    But now I want to call my php script like this:
    Code:
    myscript.php?url=URL OF PAGE
    Url of page I got with:
    var url = window.location.href;
    but now how to do something like this:

    Code:
    <script language="JavaScript" >
    var url = window.location.href; 
    </script>
    <script language="JavaScript" src="myscript.php?url="+url type="text/javascript"></script>
    This doesnt work.
    Please help me.

  2. #2
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi nikola8,

    If you are using an HTML doctype you can do this:
    Code:
    <script type='text/javascript'>
    document.write(
      "<script type='text/javascript' src='myscript.php?url=" + encodeURIComponent(window.location.href) + "'><\/script>"
    );
    </script>
    You can do this for either HTML or XHTML doctypes:
    Code:
    <script type='text/javascript'>
    (function() {
      var scr = document.createElement('script');
      scr.type = 'text/javascript'; // is your php file returning Js?
      scr.src = 'myscript.php?url=' + encodeURIComponent(window.location.href);
      document.getElementsByTagName('head')[0].appendChild(scr);
    })();
    </script>

  3. #3
    SitePoint Zealot nikola8's Avatar
    Join Date
    Jun 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks MikeFoster.

    I put this code:
    Code:
    <script type='text/javascript'>
    function Checker() {
      var scr = document.createElement('script');
      scr.type = 'text/javascript'; 
      scr.src = 'http://mypage.com&url=' + encodeURIComponent(window.location.href);
      document.getElementsByTagName('head')[0].appendChild(scr);
    };
    Checker();
    </script>
    But now problem is that I see that element on the end of html page.
    I want to see it on the same place where I puted javascript code.
    Is it possible ?

  4. #4
    SitePoint Zealot nikola8's Avatar
    Join Date
    Jun 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My php script return java code:

    echo 'document.write (\'SOME STRING\')';

  5. #5
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you're already using "document.write" then use my first example.


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
  •