Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

How to access Harlowe variables in Javascript

Hello!
I'm trying to make some randomization with images and it would be super useful to access variables I set in Harlowe inside the Javascript, but I can't find out how to do that...

Here's an example of what I want:
(set: $has_book to true)

<div id="img_holder">
   <img class='overimg' src='bg.png'>
</div>
<script type="text/javascript">
   var images = ['left','right','left2','right2'];
   if ($('$has_book').value==101)
   {
      $('#img_holder').append("<img class='overimg' src='" + images[Math.floor(Math.random() * images.length)] + ".png'>");
   }
</script>


So, the idea is to access $has_book from inside the Javascript, is that possible?
Thank you

Comments

  • Harlowe currently does not have a documented Javascript API, and it's Javascript engine has been deliberately designed/built in a way that limits access to it's internals.

    Some people have found novel ways (hacks) to get around Harlowe's restrictions, like csalzman's popular solution (and my slight correction to it) in the Any other user input methods available? thread.

    re: is that possible?

    Maybe, it can depend on a number of things.

    eg. Have you displayed the variable's value on the current page using markup or HTML tags that can be found using DOM node accessing techniques.
    (set: $health to 100)
    
    $health
    
    <script>
    	setTimeout(function(){
    		var value = $("tw-expression[type='variable'][name='health']").text();
    		console.log(value);
    	}, 50);
    </script>
    
Sign In or Register to comment.