Use .Net Resource in function with separate java script file

坚强是说给别人听的谎言 提交于 2020-04-30 07:01:46

问题


I want use functions of script , like function login() , the result of login will display message for end user from .Net Resource because we support multi language.

I want using separate java script file not in the same view , because I already know how I can do it by this script

// this is View

<script>
alert(@Resource.Layout.Msg)
</script>

but what really want to do: use this function in separate file

fucntion login(){

if($("#user").val()=="hi")
{
 alert(@Resource.Layout.Msg)
}
}

回答1:


You could define a global variable on a separate script tag and it would still be accessible on an external javascript file.

In your view;

// define a global variable in this script tag
<script>
   var text = "@Resource.Layout.Msg";
</script>
// call an external script that uses the variable text
<script src="Link to your script" />

Then in your external script;

function Login(){
   alert(text); // use the variable defined on separate script
}


来源:https://stackoverflow.com/questions/61318084/use-net-resource-in-function-with-separate-java-script-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!