JS script is only working when placed in partial view - javascript

I have a partial view that is loaded via an ajax call. Inside that partial, I have a textarea that is modified by a script. The script is used in other places across the site and is referenced in the _layout page...
<script src="~/Scripts/ckeditor/ckeditor.js"></script>
The script isn't working unless I place it in the partial view.
Its a ckeditor script for what its worth...
http://docs.ckeditor.com/#!/guide/dev_installation
This is not specific to this script though - it seems any script that needs to fire onload will fail on a run-time loaded partialview.
I would think that I should leave the script in a universal location like _layout rather than place it both in the _layout and add it to every partial its needed in.
Any ideas?
In my case, the script works by applying a class to a textarea...
#Html.TextAreaFor(model => model.business.act_comments, new { #class="ckeditor" })

Related

How to dinamically load page with JQuery and Codeigniter?

I'm using Codeigniter and Jquery with a basic template that works fine and loads my javascripts and css assets normally.
$this->load->view('header');
$this->load->view($content);
$this->load->view('footer');
In some content pages I have a link that loads the view by using on click function that calls the controller .
$(#linkid).on('click',function(){
url='url/path/to/controller/method';
$('#targetdiv').load(url);
}
The problem in using the code above, despite the page looks like embedded on template, it works like a independent page forcing me to duplicate tags on included page in order to make Jquery works fine, causing duplicated modals and overload the application. Any Idea to solve this?

Rails page-specific JavaScript not recognizing jQuery

I have some JavaScript I have written for one of my _forms I placed it in shows.js.coffee and, at the bottom of the form included = javascript_include_tag 'shows'
When the page loads I get the $ not recognized error. If I move the include tag to my /layouts/application.html.haml and include it after I load my application.js then my JavaScript executes fine, but is not loaded onto every page. What do I need for it to just be loaded into the one page that needs it?

JavaScript/jQuery doesn't work on Editor/Display templates loaded with Ajax

I created editor and display templates for my entire object downline.
Now one of the inner objects' text boxes should be a date-picker.
The thing is it's loaded after the initial loading using Ajax, and the included scripts don't work.
I tried to include the following script on the bottom of the template, the script renders but does nothing:
#Styles.Render("~/Content/themes/base/css")
#Scripts.Render("~/bundles/jquerytimepicker")
<script type="text/javascript">
alert('dddddddddddddddddddddddddd');
$('.time-picker').timepicker();
</script>
In the code abve I also added a dummy alert just to prove that the script isn't running, which makes the question much simpler: "How to run scripts from templates and partial views?".
I tried to include the scripts in a section define in the outer page, but that doesn't work, as templates and partial view cannot include sections by design (Read this).
it doesn't work because the js that attaches the timepicker to the html runs at document.ready, but the content loaded via ajax is loaded after document.ready
so you need to call the method that attaches the timepicker again after the ajax request has completed
you can try this:
$(document).ajaxComplete(function(event, xhr, settings) {
alert('an ajax request completed');
$('.time-picker').timepicker();
});

Calling a JavaScript function from another view

I'm new to ASP.NET MVC and was wondering where JS functions for the views go. I have a table element in a view that displays the #RenderBody part of _Layout, and in it's th elements, I have -
<th id="one" oncontextmenu = "return menu(this)">label one</th>
Where should the function main(..) be located? Can it be in the script tag on the _Layout page or are individual view pages allowed to have their own script tags where this should go?
I've worked in WebForms before, so moving to MVC, I'm having trouble visualizing how to put all the pieces together. Help please!
Once you defined your script in your layout page u can use it anywhere else.there is no limitation in calling javascrip function cross-view.
It can be in the Same view file surrounded by a script tag
It can be in the Layout.cshtml file surrounded by script tag
It can be in a external javascript file referred in the same view
It can be in a external javascript file referred in the
Layout.cshtml view
Layout.cshtml works similar to Master page. so whatever you include there will available to all the views which uses the layout.

ASP.NET MVC Ajax.ActionLink

When i click on a Ajax.ActionLink, which displays a partial view, why does none of the javascritp associated with the partial view fire? This previously all worked before when I used Html.ActionLink. I have a series of scripts referenced in master page, which include $document.Ready functions. I have also tried added the script into the partial views themselves but they still don't fire. Any ideas?
javascript code dynamicly loaded into a div doesnt get recognised as executable code.
put the code in a function in a sepperate .js file, load this in your site.master and start executing by adding the
new AjaxOptions { oncomplete="myJavascriptFunction" }
to your Ajax.Actionlink

Categories