accessing javascript variables

after my previous problem i’m trying to access javascript values that should be in the dom.

i actually want to access the browser url (to get the location of the p3d file)

neighter of my approaches work:

print base.appRunner.dom.window.location.hostname
print base.appRunner.dom.location.hostname

# just to check if i get anything at all
print base.appRunner.dom.document.getElementById('title')

if i understand javascript and panda’s access to it correctly this should be possible without any javascript in the html. is this correct?

Yes, this should work. In what way does it fail?


it doesnt print anything useful or i dont understand how to access the information.

print AppRunnerGlobal.appRunner.dom.location
-> prints “BrowserObject(1938).location”

print AppRunnerGlobal.appRunner.dom.location[‘hostname’]
-> print nothing

could this be, because i’m running it before directstart?

Hmm. I tried this out too. I duplicate your results, plus one. I tried the following

from direct.showbase import AppRunnerGlobal
print 'location = %s' % (AppRunnerGlobal.appRunner.dom.location)
print 'hostname = %s' % (AppRunnerGlobal.appRunner.dom.location.hostname)
print 'href = %s' % (AppRunnerGlobal.appRunner.dom.location.href)

which produces this output when embedded in file:///Users/drose/t.html:

location = BrowserObject(35).location
hostname = 
href = file:///Users/drose/t.html

Which is, in fact, the correct results. The hostname is supposed to be empty since there is no hostname component to that URL. I agree the output of the location object itself is a little confusing–it confuses me too–though it’s not obvious what is the correct way to format a generic JavaScript object.


i got a bit confused because the following code does work in javascript.

<script type="text/javascript">

thanks a lot, i’ve been fiddling with the variables several hours already.

while we’re at the topic. how would you call a javascript function, or read a defined variable?

<script type="text/javascript">
var loc = window.location;
function getWindowLocation(){
  return window.location;

i have tried, without success:


thanks for the help

This works for me:

print 'href = %s' % (AppRunnerGlobal.appRunner.dom.getWindowLocation().href)

In what way does it fail for you?


Towards the larger confusion, of being able to alert() JavaScript objects directly from the browser, but not in Python–this was due to the fact that alert() and other string-based operations in JavaScript implicitly called an object’s toString() method to make the conversion. I’ve just added code to make the Python str() method do the same thing, so in future releases, you will indeed be able to simply print base.appRunner.dom.location and see the expected results.