pass C# parameters to JS - javascript

I have a C#-MVC project. I want to refresh the page every X second -
I put in the cshtml file the code:
<script type="text/JavaScript">
timedRefresh(X);
</script>
but I need to take X from C#, let's say it's "ViewBag.Seconds".
How can I do this?

Razor doesn't care if it's outputting HTML or javascript, so you could do:
<script type="text/JavaScript">
timedRefresh(#(ViewBag.Seconds));
</script>

If you are using the Razor syntax it can be done like this:
<script type="text/JavaScript">
timedRefresh(#(ViewBag.Seconds));
</script>
The IntelliSense may report an error or warning, but it works anyway.

It's pretty easy
<script type="text/JavaScript">
timedRefresh(#ViewBag.Seconds);
</script>

You can use an Action too.
like this:
<script type="text/JavaScript">
timedRefresh(#(Html.Action("Action","Controller")));
</script>

Related

Running JavaScript specific function from html

Abit new to html and javascript.
I got a JS script and an html code.
I want to run a specific function in my JS form my html code. how do i do that?
My JS generalRedirect.js:
var redirect = {
test1: function () {
window.alert("sometext");
},
....
My HTML:
..
<script src="#Url.Content(".../Scripts/generalRedirect.js")" type="text/javascript"></script>
I want to run a specific function in my JS form my html code. how do i
do that?
You can call your function like this:
redirect.test1();
and don't forget to put that inside <script> tags and also be sure to include the correct file.
Do this:
<script src='#Url.Content(".../Scripts/generalRedirect.js")' type="text/javascript></script>
<script>
redirect.test1();
</script>
Try to do something like this:
<script src='#Url.Content(".../Scripts/generalRedirect.js")' type="text/javascript"></script>
<script>
redirect.test1();
</script>

How to include <script type> in external .js file?

I want to include following JS in external .JS file.
<script type="text/x-serialization" data-gwd-canvas="serialization">
//------
</script>
How to accomplish this?
Thanks.
it's simple
<script src="/path/to/js" type="text/javascript"></script>
try something like this
<script type="text/x-serialization" data-gwd-canvas="serialization" src="path/to/js"></script>
Well, in that case, you can use as always the fantastic free jQuery framework, which let you do the very same thing in one line:$.getScript("my_lovely_script.js", function(){
alert("Script loaded and executed.");
// Here you can use anything you defined in the loaded script
});
I used like below
<script type="text/x-serialization" data-gwd-canvas="serialization" src="serialize.js"></script>

Scrollsnap JQuery Plugin

Hello I'm looking for someone proficient in Javascript. The site I'm using uses a javascript file to initialize the plugins and load settings and what not. So after reading some of
http://benoit.pointet.info/stuff/jquery-scrollsnap-plugin/
I decided to use it to snap to my divs(all with the class "slide")...
However placing this
$(window).scrollsnap({
snaps: '.slide',
proximity: 100
});
in my js.js file accomplished nothing
Any help is appreciated!
The code I used with that was this and it worked, give it a try:
$(document).ready(function() {
$(document).scrollsnap({
snaps: '.slide',
proximity: 100
});
});
Not sure about $(window), where did you get that from?
Also, are you linking to the appropriate javascript files and to the jquery library? Make sure to put it at the end of your html like this:
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="js/app.js" type="text/javascript" charset="utf-8"></script>
<script src="js/jquery.scrollstop.js"></script>
<script src="js/jquery.scrollsnap.js"></script>
<script src="js/jquery.fitvids.js"></script>
</body>
</html>

how Javascript call to a function of a external file?

i made a html page with this coding
<html>
<head>
<script src="../a.js">
var u=document.URL; var i='t4527878445'; m_web(u,i);
</script>
</head>
<body>
</body>
</html>
In a.js i have this code
function m_web(u,i) {
alert('l');
alert('u');
}
but my webpage is unable to call this function which is coded in an external file. i am not getting any alert with this. i don't know what is problem. plz tell me simple solution for this.
thanx in advance
A single <script> tag can link to an external resource using the src attribute OR contain inline JavaScript code, but it can't do both at the same time. If you specify a src attribute any content between the <script src="foo.js"> tag and the </script> tag is ignored.
Since you want to load the external JS file, and then execute some JavaScript code, you'll need two separate tags to do so:
<script src="../a.js"></script>
<script>
// your code
</script>
plz write your code like below
<script src="../a.js"></script> </script>
//^^^^^^^^^^^^^^^^^^ first close script tag of linked js file
<script type="text/javascript">// then call your inline jscode
var u=document.URL; var i='t4527878445'; m_web(u,i);
</script>
Try
you are not closing script tag
<script src="../a.js"></script>
^//added closing script tag
<script>
var u=document.URL; var i='t4527878445'; m_web(u,i);
</script>
to alert what you pass use
function m_web(u,i) {
alert(i);
alert(u);
}

Java Script redirect is not working in smarty .tpl file

I am developing a web application using PHP, Smarty combination, I have wrote a javascript redirect in the .tpl file, but it is not working now. Please take a look at the code and help me to find out the problem.
This is the code segment,
{literal}
<script type="text/javascript">
if(confirm({/literal}'{$info}'{literal})){
window.location = "logbook_client_section.php?e={$ckey}";
}
</script>
{/literal}
Problem is only in the redirect section, it is now redirecting to logbook_client_section_add.php?e={$ckey} , I would like to get the value of $ckey ( smarty variable) in the url.
try it
{literal}
<script type="text/javascript">
if(confirm('{/literal}{$info}{literal}')){
window.location = "oop.php?e={/literal}{$ckey}{literal}";
}
</script>
{/literal}
You are trying to use a {$var} inside a {literal} block. That cannot work...
The cleanest solution to avoid unreadable code caused by closing/opening the {literal} blocks within your code would be this:
<script type="text/javascript">
var info = '{$info}';
var ckey = '{$ckey}';
{literal}
if(confirm(info)){
window.location = "logbook_client_section.php?e=" + ckey;
}
{/literal}
</script>
You can use this instead:
var ckey='{$ckey}';
window.location = "logbook_client_section.php?e="+ckey;

Categories

Resources