Welcome to WebmasterWorld Guest from 54.227.214.52

Forum Moderators: open

Value from ASP/VB in Javascript

Value from ASP/VB in Javascript

   
6:09 pm on Nov 15, 2010 (gmt 0)

10+ Year Member



Hi,
I'm trying to call a Unique Identifier function (in ASP/VB) in my Javascript function, and the returned value is the same each time. On the other hand, when I call the ASP/VB function from separate Javascript alert boxes, the value is different each time. I would like to get a different value each time, with the function, if possible.

Does anyone know why the value would be the same? Many thanks for any help on this.

The ASP/VB:


<% 
Function GetGuid()
Set TypeLib = CreateObject("Scriptlet.TypeLib")
GetGuid = Left(CStr(TypeLib.Guid), 38)
Set TypeLib = Nothing
End Function
%>




The javascript:


<script>
function GetGuid_test_f()
{
strGetGuid_test='<%=GetGuid()%>'
//strGetGuid_test='<%=strGetGuid_test_b%>'
alert("strGetGuid_test is...."+strGetGuid_test)
}

GetGuid_test_f()

GetGuid_test_f()

GetGuid_test_f()
</script>





The Javascript that works (gives a different GUID each time)


strGetGuid_test='<%'=GetGuid()%>'
alert("strGetGuid_test is...."+strGetGuid_test)
strGetGuid_test_b='<%'=GetGuid()%>'
alert("strGetGuid_test_b is...."+strGetGuid_test_b)
strGetGuid_test_c='<%'=GetGuid()%>'
alert("strGetGuid_test_c is...."+strGetGuid_test_c)
6:58 pm on Nov 15, 2010 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



The GetGuid method runs at the server. The GetGuid_test_f method runs at the client. The reason the second bit of code works is because you're calling the server side method multiple times, while in the first example you're only calling it once. If you were to view the source of your page in the browser, you would see that your GetGuid_test_f method will have a single hard coded value. As in:

function GetGuid_test_f()
{
strGetGuid_test='12345678901234567890123456789012345678';


Is the number of times that you need to call GetGuid a fixed amount, or could it vary?
7:57 pm on Nov 15, 2010 (gmt 0)

10+ Year Member



The actual application is a "create new div" code, and the function for creating the new div sets a variable equal to the result of the GetGuid (ASP) function (below).

var strObj_GUID = '<%=GetGuid()%>';


Each time a user clicks on the "create new div" button, the same code will be called. So, the number of times is widely variable.
Is the GetGuid function not accessed more than once (as it returns the same result each time)?
Thanks again for your help.
8:38 pm on Nov 15, 2010 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Yes, each time the button is clicked the JavaScript (client side) code will execute. The ASP (server side) code, though, would only have been executed once when the page was initially accessed on the server. The output of that initial call was used to populate a value in the client side code, but calling that client side code does not cause the server side code to execute again.

If you need to get the value from ASP code, then you may want to use AJAX to send another request to the server. However, you should evaluate whether you want a solution that will work for users that have JavaScript disabled as well.
11:20 pm on Nov 15, 2010 (gmt 0)

10+ Year Member



Many thanks - I think it's a little clearer now.
I decided to append the ASP UUID string with the current date from js, incl milliseconds, to make it unique.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month