Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
Hi Guys,

I am facing a problem to use javascript function, Please help me.
I am using below javascript code:

HTML
<script>  function Spanish() {

            setCookie("es");
            var url = window.document.location.href.replace("#googtrans(en|en)", "").replace("#googtrans(en|es)", "");
            window.open(url + "?lang=es#googtrans(en|es)", "_self");
            location.reload();
            var result = '<%= ELearn.SetUserLanguage("es",Session["uid"].ToString())%>';
            alert(result);
        }

</script>

        <script>

            function English() {
                setCookie("en");
                var url = window.document.location.href.replace("#googtrans(en|es)", "").replace("#googtrans(en|en)", "");
                window.open(url + "?lang=en#googtrans(en|en)", "_self");
                location.reload();
                var result = '<%= ELearn.SetUserLanguage("en",Session["uid"].ToString())%>';
                alert(result);
            }

        </script>


I am using these functions as below:
HTML
 <a id="EnglishLink" href="" önclick="English" style="text-decoration:none;">English</a>
<a id="SpanishLink" href="" önclick="Spanish" style="text-decoration:none; ">Spanish</a>


But it is called without pressing on link.'
Please help me.

Thnaks.
Posted
Updated 28-May-14 6:04am
v2
Comments
Richard C Bishop 27-May-14 16:05pm    
Something is calling it. Show the markup please.
ZurdoDev 27-May-14 16:19pm    
As Richard said, something has to be calling it. Just search for it. Should be easy to find.
Sumit Kumar Singh India 27-May-14 16:43pm    
When i have pressed on the link, alert is coming. But when it is called automatically alert not coming and c# function ELearn.SetUserLanguage called. I am not able to find where is the fault.
Sergey Alexandrovich Kryukov 28-May-14 1:48am    
What is the parent HTML element of the "script", head or body?
—SA
Sumit Kumar Singh India 28-May-14 10:11am    
When i put in body then only this happened. When i put this javascript code in head then its not called automatically but the c# function not working in head.

Sumit Kumar Singh India wrote:
When i put in body then only this happened. When i put this javascript code in head then its not called automatically but the c# function not working in head.
That's why I asked. This statement itself contains the answer to the question.

This behavior is correct and is the part of HTML standard: if you want some script to elaborate when some function is called, put it in the <head> element. What you put a script in <body>, it is executed automatically. A script in the <head> is like a library, a script in the <body> is like entry point. This is something important to know if your do scripting.

—SA
 
Share this answer
 
v2
Comments
Volynsky Alex 28-May-14 15:49pm    
Good answer!
Sergey Alexandrovich Kryukov 28-May-14 17:41pm    
Thank you, Alex.
—SA
Volynsky Alex 28-May-14 18:29pm    
:) You're welcome Sergey Alexandrovich!
If I understand your problem correctly, these are your culprits.

var result = '<%= ELearn.SetUserLanguage("es",Session["uid"].ToString())%>';

var result = '<%= ELearn.SetUserLanguage("en",Session["uid"].ToString())%>';


Your ELearn.SetUserLanguage is getting executed at the time the page is generated, not when your link is clicked. If you only want SetUserLanguage to be called after the link is clicked, you're going to need to change it in to a web method.

If you're not familiar with web methods, this appears to be a decent write up:
http://www.morgantechspace.com/2014/01/Call-Server-Side-function-from-JavaScript-in-ASP-NET.html[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900