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
Related
I have a Partial page in which I am loading div elements. I want to call a JavaScript function when each div is loaded in my partial view.
<p class="m-0" onload="markMessageAsDelivered('#item.isDelivered')">#item.Message</p>
This is the function I have in my JS in my razor page.
function markChatAsDelivered(isDelivered) {
if (!isDelivered)
alert('markChatAsDelivered');
}
This function is not getting called. I tried to put it in windows onload as well but still it is not working. Please let me know what can be done?
The problem I've had with these things is that onload is called before ASP.NET makes its changes to the page. There's two solutions I use:
$(document).ready() with JQuery waits until the DOM is fully loaded and done, so you can migrate some code into there.
function pageLoad(sender, args) is a built-in ASP JS function (like Page_Load in your code behind) that gets called when the page is done being loaded. You can migrate some JS into here like the first answer.
Personally, even though I like to avoid libraries, I like the first option because you can use more than one per page. This is only really useful if you're calling code in your master page though. The second option is good if you only use it once per page, but obviously if you try to use it in both your master page and current page, it won't work properly. So I'd recommend JQuery, but if you must avoid it, pageLoad works too.
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?
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" })
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.
I created a phonegap application, the first page navigates to the second page by "href".
in the second page, I have a JavaScript function that I want to execute.
The problem is that the page doesn't know the JavaScript, just if I copy the function to the
first page - it works.
I include the JavaScript in the page.
what is the problem?
For each page in the application you will have to include the JavaScript code you wish to execute on that page. For instance on page1.html you have a referenced function called getData() you will be able to call it on page1.html. If you follow a href to page2.html the function getData() is now out of scope. It is worth mentioning this is exactly how things work in web browsers.
The way around this is to move getData() to an external JavaScript file like main.js. Then you reference main.js via a script tag in both page1.html and page2.html. Now you'll be able to call getData() from either page.
Done use anchor tags with href values to change pages.
Use the jquery mobile function changePage http://jquerymobile.com/test/docs/api/methods.html
to change pages, as changePage wont load a new html fully but would load just the topmost jquert mobile "page" in that html file into the dom.