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>
Related
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);
}
In head I have:
<script type="text/javascript" src="/categ.js"></script>
In categ.js, I have:
<script type="text/javascript">
var gallery=new sim({
wrapperid: "gallery1"
..................
</script>
In body I have:
<div id="gallery1"></div>
When I load page, the script is not called, because is in extended file. If I paste it directly in head - it's works.
So, How I can call from "body" with some tag the function in categ.js
Try without
<script type="text/javascript">
...
</script>
inside the categ.js file.
Then put:
window.onload = function() {
// the JS code you want to execute
};
in the file. If you use jQuery you can replace this with:
jQuery(document).ready(function() {
// the JS code you want to execute
};
So in case you want to create a new object on page load you execute this code:
var gallery=new sim({
wrapperid: "gallery1",
foo : "bar"
});
You can try calling it with window.onload to ensure it runs on page ready:
window.onload = gallery;
You can check whether your tag is ready for using with your js by calling window.onload or use jquery
I am trying to call innerHtml in JavaScript its working in the same file but not in the separate JavaScript JS file.
my working code is
<script type="text/javascript">
function my()
document.getElementById("abc").innerHTML="hello";
}
</script>
<div id="abc" onmouseover ="my()"> hi hw ru </div>
But if I invoke this method in separate JavaScript file its not working even I am giving the source path of the JavaScript file like
<script type="text/javascript" src="js/framemrq.js">
Missing the function keyword
<script type="text/javascript">
function my(){
// Your code here
}
</script>
please define your my function correctly like this:
function my() {
document.getElementById("abc").innerHTML="hello";
}
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>
Is there a way of calling a function that exists in a HTML file from a .js file?
Something like this:
HTML file:
<script type="text/javascript" src="javaFile.js"></script>
<script type="text/javascript">
function doSomething() {
};
</script>
javaFile.JS file:
htmlFile.doSomething();
? Thanks :)
You can directly call doSomething();