Click to See Complete Forum and Search --> : Accessing JavaScript variable in custom tag


dcastrapel
May 21st, 2002, 10:39 AM
Hello,

Is it possible to access a JavaScript variable and use its value as an attribute for a custom tag? For example:

<script>
var test = "test"

<mytag:test attr="WANT TO USE VALUE OF test HERE" />
</script>

Thanks in advance!

susad
May 21st, 2002, 01:00 PM
You could do something like this:

<script>
test = "a test-value";
document.all.myid.setAttribute("href", test, true);
</script>

<a href="" id="myid">...</a>

lerouxjcg
May 22nd, 2002, 04:45 AM
Hi Susad,
Thanks for the reply. When I tried it your way I got the
following error: document.all.myid is null or not an object.

I have tried everywhere to get an answer for this but
without success.

Regards
lerouxjcg

dcastrapel
May 22nd, 2002, 10:22 AM
Yes, thanks to all for the replies. I too get an error when I try the method suggested by Susad. I think that the basic problem is that what I want to do is mix server-side data (custom tags and java scriptlets) with client-side data (JavaScript) and it's not possible to do that dynamically. I ended up just using a form and post method to reload the page content from the server.

Thanks again for all the ideas.

susad
May 22nd, 2002, 11:09 AM
You got the "document.all.myid is null or not an object" error msg because the a-Tag (or whatever document.all.myid should be) still isn't loaded. You should encapsulate this into a function like this:


<script>
function func(value)
{
if (document.all.myid != null)
document.all.myid.setAttribute("href", value, true);
else
setTimeout("func("+value+");", 500);
}
func("something");
</script>


In this example you call the function func. And this function checks whether the a-Tag exists (that means it checks whether the document is loaded complete). If not it calls itself after a timeout (500 ms in this case) until document.all.myid is available.