Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
My probelm is HTML javascript is not working in an aspx page, but I want to run code in aspx.


My HTML code is:

HTML
<html>
<head>
    <title></title>
</head>
<body>

<div style="text-align:center"> 
  <button  önclick="playPause()">Play/Pause</button> 
  <button  önclick="makeBig()">Big</button>
  <button  önclick="makeSmall()">Small</button>
 <button  önclick="makeNormal()">Normal</button>
 <input type="text"  id="t1"   önblur="setTime()"/>
  <select name="hai" id="hai1" >
  <option value="0.5">0.5kbps</option>
  <option value="1.0">1.0kbps</option>
  <option value="1.5">1.5kbps</option>
  <option value="2.0">2.0kbps</option>
   </select>
   <button  önclick="bitrates()" >play</button>
  <br> 
  <video id="video1" width="420" controls="controls" src="video/ram.MP4">
  </video>
</br></div> 

<script type="text/javascript">
    var myVideo = document.getElementById("video1");


    function playPause() {
        if (myVideo.paused)
            myVideo.play();

        else
            myVideo.pause();
    }

    function makeBig() {
        myVideo.width = 600;
    }

    function makeSmall() {
        myVideo.width = 320;
    }

    function makeNormal() {
        myVideo.width = 420;
    }
    function bitrates() {

        myVideo.playbackRate = document.getElementById("hai1").value;
    }

    function setTime() {

        myVideo.currentTime = document.getElementById("t1").value;
    }


   
</script> 

</body>
</html>


Please help me, how to use javascript in aspx file.
Posted
Updated 9-Oct-13 5:39am
v2
Comments
ZurdoDev 9-Oct-13 10:35am    
Just use a script tag to import the js file
<script src="Common/Scripts/jquery.min.js" type="text/javascript"></script>
or if you want the js right in your aspx just use a <script> tag.
Sergey Alexandrovich Kryukov 9-Oct-13 13:00pm    
It won't solve the problem, which is different: the script is not doing anything except creation of some objects.
Please see my answer (tested to make sure it works).
—SA
ZurdoDev 9-Oct-13 14:08pm    
You understood the OP? Wow. :)
Sergey Alexandrovich Kryukov 9-Oct-13 14:10pm    
:-)

1 solution

This is just because your script is not doing anything which could be manifested as "work". Let's see: first, it obtains the reference to the DOM element "video", correctly. Then you create 6 functions. You simply create 7 objects, nothing else. After that, the scripts runs out of its scope, and eventually, all those 7 objects will be garbage-collected. That's all. The problem is: none of your functions is called. You should call at least one, at the end of your script text, and it should bootstrap all the functionality.

Usually, the script being a child of your <body> element is used to add event handlers to some events of some DOM elements. Even after your scrip is finished and runs out of its scope, the elements with event handlers added to some events of those HTML elements will stay in the browser's memory until you close the whole page. It means that your handler functions, too, will be kept in browser's memory and will respond to events triggered by the user. This is how it all works.

In case of video, the minimal script based on your code will work if you do this:
XML
<script type="text/javascript">

    myVideo = document.getElementById("video1");

    function playPause() {
        if (myVideo.paused)
            myVideo.play();
        else
            myVideo.pause();
    }

    playPause();

</script>


Pay attention for the last line, this is what makes it work.

Good luck,
—SA
 
Share this answer
 
v2

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