1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Help with a JS code please...

Discussion in 'JavaScript' started by n00bl3t, Mar 13, 2014.

  1. #1
    I have this code:

    var url='externaldomain.com/stats.php';
    var e = document.createElement('script');
    e.src = document.location.protocol + url;
    e.async = true;
    document.getElementsByTagName('body')[0].style.display = "block";
    document.getElementsByTagName('head')[0].appendChild(e);
    Code (markup):
    but whenever I insert this into a page...

    the url that is being called is like this:

    maindomain.com/externaldomain.com/stats.php


    How do I make it call the external url?:
    externaldomain.com/stats.php
     
    Solved! View solution.
    n00bl3t, Mar 13, 2014 IP
  2. startdream

    startdream Member

    Messages:
    76
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    28
    #2
    Please change url
    var url='externaldomain.com/stats.php';
    To
    var url='stats.php';
     
    startdream, Mar 14, 2014 IP
  3. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #3
    Unless the stats.php-file is on your own server/domain, you need to prefix the url with http:// to access an external file via http://
     
    PoPSiCLe, Mar 14, 2014 IP
  4. n00bl3t

    n00bl3t Well-Known Member

    Messages:
    383
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    120
    #4
    how can this work if the file is outside of this site/server...


    Hi,

    I tried that too... but the url just extended and added the http:// like this....

    maindomain.com/http://externaldomain.com/stats.php
     
    n00bl3t, Mar 14, 2014 IP
  5. Tomve

    Tomve Active Member

    Messages:
    18
    Likes Received:
    1
    Best Answers:
    2
    Trophy Points:
    70
    #5
    I'm not pro in js and it's maybe stupid but what this?

    var url='http://externaldomain.com/stats.php';
    var e = document.createElement('script');
    e.src = url;
    ...
    Code (markup):
    my output is:

    http://externaldomain.com/stats.php
    Code (markup):
     
    Tomve, Mar 14, 2014 IP
  6. #6
    document.location.protocol doesn't contain slashes. I would suggest NOT using it as what you plug into the script creation since you might also have file: in local testing as a result.

    var
    	url = 'externaldomain.com/stats.php',
    	d = document,
    	e = d.createElement('script');
    	
    e.src = (
    	d.location.protocol == 'https:' ? 'https://' : 'http://'
    ) + url;
    e.async = true;
    d.getElementsByTagName('head')[0].appendChild(e);
    Code (markup):
    Should do the trick. That way it will auto-detect http or https, which I'm assuming was your intent. That's pretty much how google does it with their various scripts (like analytics) to make sure they auto-detect.

    Also, you don't have to look up BODY that way. document.body is functionally identical to that lookup. (though you do need to do it to HEAD)

    ... and please for the love of ghu tell me you aren't pissing all over the page's accessibility by starting out with BODY set to display:none... You do realize scripting off that gives users a blank page, right?... and for what? likely some silly 'try to make it LOOK like it's faster' by throwing more code at it nonsense? Just saying...
     
    Last edited: Mar 14, 2014
    deathshadow, Mar 14, 2014 IP
    n00bl3t likes this.
  7. n00bl3t

    n00bl3t Well-Known Member

    Messages:
    383
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    120
    #7
    @
    deathshadow

    that worked...

    thanks a lot!

    the reason for body display:none

    so that.. if that certain script gets remove... the page will not be accessible/copied...
     
    n00bl3t, Mar 14, 2014 IP
  8. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #8
    You know that the code is still displayed in "view: source", right? Regardless of what kind of "display: none" you're using. A quick click in console in webdeveloper in Firefox will show all the content regardless...
     
    PoPSiCLe, Mar 15, 2014 IP
  9. n00bl3t

    n00bl3t Well-Known Member

    Messages:
    383
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    120
    #9
    of course i know that...
    but also know a way to put all codes into a "ONE LINE"...

    so yeah... have fun trying to put all the codes together... :)
     
    n00bl3t, Mar 15, 2014 IP
  10. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #10
    Uhm... again, a simple splitter and beautifier will fix that with a simple copy/paste - or if you're using proper plugins, by a click of a button...
     
    PoPSiCLe, Mar 15, 2014 IP
  11. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #11
    That's kind of the point of why it's stupid, it's so easily slapped aside by even the simplest of means that anyone who wants to copy the content that sure as shine-ola isn't going to do it. Pissing away your accessibility and graceful degradation in some deluded attempt to stop people from doing something you CANNOT stop them from doing EVER on a website is... well... I lack the words in polite company.

    Whoever gave you the notion this was something you should ever do on a website needs a good pimp slap. Remember the unwritten rule of javascript; if you can't make a page that works without javascript first, you have no business adding scripting to it. Scripting off a blank page is REALLY stupid with mobile users disabling scripting to save battery life, users on metered connections turning off/blocking scripting to save bandwidth, and the complete lack of trust for javascript that results in browser extensions like NoScript having several million users.

    Particularly when it can be slapped aside by anyone who would want to copy the content in the first place with ease.
     
    deathshadow, Mar 16, 2014 IP