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
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://
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
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):
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...
@ 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...
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...
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...
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...
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.