CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 2002
    Location
    Vancouver, WA
    Posts
    2

    Question Accessing JavaScript variable in custom tag

    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!

  2. #2
    Join Date
    Jan 2002
    Location
    Germany
    Posts
    49
    You could do something like this:

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

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

  3. #3
    Join Date
    May 2002
    Location
    Pretoria, South Africa
    Posts
    3
    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

  4. #4
    Join Date
    May 2002
    Location
    Vancouver, WA
    Posts
    2
    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.

  5. #5
    Join Date
    Jan 2002
    Location
    Germany
    Posts
    49
    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:

    Code:
    <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.
    susad

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured