locked
How to integrate javascript functions or .js files in a silverlight project? RRS feed

  • Question

  • Hello All,

    I am working on silverlight.

    Recently I came across some functions in javascript which work well when included in an aspx project.

    But when I make changes in the aspx page of my silverlight project, the javascript file is not getting invoked. I have made changes only to the aspx page of my silverlight project.

    What changes are needed to be made in the XAML file of the silverlight project.

    Please reply if any of you have sorted this out..! :)

    Thanks & Regards,

    Ruchita

    Monday, February 25, 2013 8:14 AM

All replies

  • Hi,

    Just include that Javascript file to the html or aspx page on which your Silverlight control is hosted. It should work then.

    The following article is about how to call JavaScript functions from Silverlight, please try to refer to:

    http://www.dotnetcurry.com/ShowArticle.aspx?ID=298 .

    Best Regards.


    Amy Peng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, March 1, 2013 4:53 AM
    Moderator
  • Thank you Amy Peng for replying..

    But my problem here is, what must be written in XAML so that the changes of the .js file is reflected in our page.

    For eg. I have a adbanner.js file which contains JavaScript functions..

    I don't want it to work on any button click..

    It should be seen & start animating as soon as the page gets loaded.

    Please help..

    Regards,

    ~Ruchita

    Friday, March 1, 2013 5:27 AM
  • I don't want it to work on any button click..

    It should be seen & start animating as soon as the page gets loaded.

    Please help..

    Regards,

    ~Ruchita

    Hi,

    If you want to call your javascript in pageload, please try to refer to:

    Assume you have a JavaScript function on an aspx page as below:

    SilverlightTestPage.aspx

    <script type="text/javascript" language="javascript" >
             function callmeonPageLoad() {
                 alert("Hello Javascript from Silvertlight");
             }  
    </script>



    If you want to call this JavaScript function on a page load of the Silverlight page then you will have to add the namespace:

    JavaScript in Silverlight 4.0

    And in the constructor of the page call it as below:

    MainPage.xaml.cs

    using System.Windows.Controls;
    using System.Windows.Browser;
     
    namespace SilverlightApplication7
    {
       [ScriptableType]
        public partial class MainPage : UserControl
        {
            public MainPage()
            {
                InitializeComponent();
                HtmlPage.RegisterScriptableObject("MainPage"this);
                HtmlPage.Window.Invoke("callmeonPageLoad");          
                
            }      
        }
    }


    For more information, please try to refer to:http://www.c-sharpcorner.com/uploadfile/dhananjaycoder/working-with-javascript-in-silverlight-4-0/ .

    Best Regards.


    Amy Peng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Friday, March 1, 2013 6:06 AM
    Moderator
  • Hey thanks..

    Got the idea how to load the function on page load.

    But this was a simple function which contained only a message box.

    My .js file is not going to contain a pop-up or message box..

    It is going to contain a img slider like the one in this site : 

    http://www.menucool.com/javascript-image-slider

    I have downloaded the .js file from this site & trying to embed in my silver-light project.

    Please reply..:)

    Regards,

    ~Ruchita

    Friday, March 1, 2013 6:56 AM
  • Hi,

    I am sorry for the late reply.

    You can just use the js file in <script src="../JS/*.js" type="text/javascript"></script>, then you can call the funtion as you want.

    Best Regards,
    Amy Peng


    Amy Peng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, March 6, 2013 1:39 AM
    Moderator
  • Hey Its OK..

    I am failing to invoke the JavaScript function..

    I am still stuck up with the jquery/javascript + silverlight integration!!

    From the menucool.com, I have downloaded the image slider & then..
    I have added the js function & the necessary cs function by clicking on "Add Exixting Item" & the I have pasted the html code in the aspx file of my silverlight project.
    Here what happens is : the xap is already loaded in & below it the banner is seen.
    Hence I must do something that loads the js function before the xap gets created, something
    like ScriptableMember or ScriptableObject.
    Please help me out......:)

    Thanks & Regards,
    Ruchita

    Wednesday, March 6, 2013 8:01 AM