The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. April 26, 2019, 03:35:25 AM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Remotely hosted JS  (Read 5186 times)
Bompa
Administrator
Lifer
*****
Offline Offline

Posts: 564


Where does this show?


View Profile
« on: March 11, 2013, 08:11:44 PM »

I know almost nothing about javascript, but I have read about the
same domain policy.

I want to host my .js on another domain of mine (for personal reasons), but when
I try, it does not execute in the context of MY domain. It executes in the
context of the remote host.

I am looking for the absolute simplest code for this. No frameworks, classes,
jquey, etc etc.  This is already giving me a headache, I don't need more
learning curves.

In order to test in which context it is executing, I use document.write(document.cookie)
on the remotely hosted domain and just call that script from my primary domain.

CODE ON MY CALLING, PRIMARY DOMAIN:
Code:
<script type="text/javascript" src="http://remote.local/cookie2.js"></script>


CODE ON MY REMOTE DOMAIN:
Code:
document.write("Cookies: "+document.cookie)

I should see the cookies that are from my primary (calling) domain, but
I see nothing since I do not have cookies set from the remote domain.

Both domains are on localhost running WAMP.

This must be possible since stats sites like statcounter.com can call code that
they host from my pages and get all sorts of info about my users.

Am I missing something?
Logged

"The most beautiful and profound emotion we can experience is the sensation of the mystical..." - Albert Einstein
Bompa
Administrator
Lifer
*****
Offline Offline

Posts: 564


Where does this show?


View Profile
« Reply #1 on: March 11, 2013, 09:35:13 PM »

nevermind.  I think it is just cookies that will not cross domain, like
statcounter can get visitor info, but can not get my login cookies
to my own site.

out
Logged

"The most beautiful and profound emotion we can experience is the sensation of the mystical..." - Albert Einstein
Bompa
Administrator
Lifer
*****
Offline Offline

Posts: 564


Where does this show?


View Profile
« Reply #2 on: March 12, 2013, 12:05:19 AM »

CHANGE TOPIC: THIS QUESTION THE PAGES ARE SAME SITE

Another JS question:

Shouldn't this work?

Code:
<html><head></head><body>
<script>

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
    if (xhr.readyState == 4) {
        alert(xhr.responseText);
        var response = xhr.responseText;
    }
}
xhr.open('GET', 'http://wordpress.local/31/', true);
xhr.send(null);

document.write(response);
</script>

TESTING XMLHttpRequest
</body></html>

From my googling, that should load "http://wordpress.local/31/" and display it.

In this case, the requesting page and target page are in the same domain (on localhost).

My result is "TESTING XMLHttpRequest" loads in the normal window and I also
get a little popup window which is blank except for the OK button.

What gives?

Bompa
« Last Edit: March 13, 2013, 03:30:46 AM by Bompa » Logged

"The most beautiful and profound emotion we can experience is the sensation of the mystical..." - Albert Einstein
Bompa
Administrator
Lifer
*****
Offline Offline

Posts: 564


Where does this show?


View Profile
« Reply #3 on: March 12, 2013, 12:19:53 AM »

BTW, I checked with liveHeaders and the request is being made and
the reponse headers are present. Possibly I am not extracting the
content properly.
Logged

"The most beautiful and profound emotion we can experience is the sensation of the mystical..." - Albert Einstein
DangerMouse
Expert
****
Offline Offline

Posts: 244



View Profile
« Reply #4 on: March 12, 2013, 06:02:05 PM »

I believe XHR requests have the similar cross domain origin rules to cookies. There are ways around this though that emulate XHR, but are a different implementation - dynamically writing <script> tags I believe or similar.

DM
Logged
Bompa
Administrator
Lifer
*****
Offline Offline

Posts: 564


Where does this show?


View Profile
« Reply #5 on: March 13, 2013, 03:32:20 AM »

I believe XHR requests have the similar cross domain origin rules to cookies. There are ways around this though that emulate XHR, but are a different implementation - dynamically writing <script> tags I believe or similar.

DM

Thanks for the reply DM. I have confused the thread though.

My OP was about remotely hosted JS, but my question about XHR
is same site.  I have just edited that question to be more clear.

Bompa
Logged

"The most beautiful and profound emotion we can experience is the sensation of the mystical..." - Albert Einstein
DangerMouse
Expert
****
Offline Offline

Posts: 244



View Profile
« Reply #6 on: March 13, 2013, 02:05:55 PM »

I'm useless at JS - but my quick method of trouble shooting for what its worth is to alert(response) and see what you get back - it could be the DOM document.write() that's causing a problem.

DM
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #7 on: March 15, 2013, 01:25:27 PM »

Wow Bomps, how did I miss this thread?

XMLHTTPRequest is extremely jealous and won't let you do what you're looking to do. I'm going to find my thread here on avoiding the issue with another mechanism ...

Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #8 on: March 15, 2013, 01:59:11 PM »

Here we go: http://www.perkiset.org/forum/ajax/itís_time_to_dump_xmlhttprequest-t336.0.html

I think that may poke you in the right direction. Wink
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
Bompa
Administrator
Lifer
*****
Offline Offline

Posts: 564


Where does this show?


View Profile
« Reply #9 on: March 15, 2013, 04:34:42 PM »

Wow Bomps, how did I miss this thread?

I thought you were out of town.

Quote
XMLHTTPRequest is extremely jealous and won't let you do what you're looking to do. I'm going to find my thread here on avoiding the issue with another mechanism ...

Thank you. I will dig into the concepts in that thread.


Bomps
Logged

"The most beautiful and profound emotion we can experience is the sensation of the mystical..." - Albert Einstein
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #10 on: March 15, 2013, 04:50:19 PM »

I was but not off the grid. Thought I was watching that stuff.

Guess I was too busy looking at PinkHat and the beach Wink

Ping here or private if you need more on that stuff. I think it's right up your alley.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
taky
Rookie
**
Offline Offline

Posts: 13


View Profile
« Reply #11 on: August 06, 2014, 01:29:42 AM »

if you want to be a rebel and not use localstorage

example_server.iced

Code:
# vim: set expandtab tabstop=2 shiftwidth=2 softtabstop=2
# example_server.iced
_ = require 'taky'
mem = require 'memory-cache'

app = _.app bare:yes

_format = (cb,val) -> "try{#{cb}(#{val})}catch(e){}"

app.get '/get', (req,res) ->
  res.set 'content-type', 'text/javascript'

  cb = req.query.cb or '__callback'

  if !req.query.key

    output = JSON.stringify {
      ok: no
      err: 'Missing key'
    }

    return res.end _format(cb,output)

  key = "#{req.ip}:#{req.query.key}"
  hit = mem.get key ? null

  output = JSON.stringify {
    ok: yes
    result: hit
  }

  res.end _format(cb,output)

app.get '/put', (req,res) ->
  res.set 'content-type', 'text/javascript'

  cb = req.query.cb or '__callback'

  if !req.query.key or !req.query.val

    output = JSON.stringify {
      ok: no
      err: 'Missing key/value'
    }

    return res.end _format(cb,output)

  key = "#{req.ip}:#{req.query.key}"
  val = req.query.val

  mem.put key, val

  output = JSON.stringify ok:yes
  res.end _format(cb,output)

app.listen 80
log ":80"


example_client.iced
Code:
# vim: set expandtab tabstop=2 shiftwidth=2 softtabstop=2
# example_client.iced
jsonp = require 'jsonp'
qs = require 'querystring'

server = 'myserver.com'

opts =
  param: 'cb'
  timeout: 3000

module.exports = store =

  get: (key,cb) ->
    url = [
      location.protocol
      '//'
      server
      '?'
      qs.stringify {key:key}
    ].join ''

    jsonp url, opts, cb

  put: (key,val,cb) ->
    url = [
      location.protocol
      '//'
      server
      '?'
      qs.stringify {key:key,val:val}
    ].join ''

    jsonp url, opts, cb


call it like this

Code:
client = require './example_client'

log "Setting a value for 'key_example'"

# set a value
await client.put "key_example", new Date().getTime(), defer()

# get a value
await client.get "key_example", defer e,val
if !e
  console.log "The value is #{val}"

see alsos:
https://github.com/maxtaco/coffee-script
https://www.npmjs.org/package/jsonp
https://www.npmjs.org/package/memory-cache

Logged

No links in signatures please
Bompa
Administrator
Lifer
*****
Offline Offline

Posts: 564


Where does this show?


View Profile
« Reply #12 on: August 06, 2014, 02:07:26 AM »

thanks and nice to see you posting here

 Applause
Logged

"The most beautiful and profound emotion we can experience is the sensation of the mystical..." - Albert Einstein
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!