UpdatePanel + jquery using on() - updatepanel

I've been experiencing the famous problem of jQuery not functioning after a postback. So I've done some research and the best new way is the live() function by jQuery. But turns out this one has been deprecated since version 1.7 and replaced with on() function.
So I transformed my jQuery plugin to use the on() function, but it still doesn't work after postbacks.
The plugin:
$(document).ready(function () {
$('.drag').on("mouseover", function () {
AfterPostBack();
$(this).draggable()
.click( function () {
$(this).draggable({ disabled: false });
}).dblclick( function () {
$(this).draggable({ disabled: true });
});
});
$('.text_label').on("blur",function () {
$(this).removeClass('focus');
});
});
var AfterPostBack = function () {
$('.drag').draggable("option", "containment", 'parent');
$('.drag').draggable("option", "snap", 'parent');
$('.drag').draggable("option", "cursor", 'move');
};
The web page:
<script type="text/javascript" src="Scripts/jquery-1.8.0.min.js"></script>
<script type="text/javascript" src="Scripts/jquery-ui-1.8.23.custom.min.js"></script>
<script type="text/javascript" src="Scripts/myplugin.js"></script>
<link href="Styles/myplugin.css" rel="stylesheet" type="text/css" />
<asp:UpdatePanel runat="server" ID="UP1" UpdateMode="Conditional" ChildrenAsTriggers="false">
<ContentTemplate>
<asp:Button ID="btn_AddText" runat="server" Text="Add Text" OnClick="AddText" />
<asp:PlaceHolder ID="ph1" runat="server">
<div class="drag">
<asp:Label ID="lbl1" class="text_label" runat="server" Text="Click Me"/>
</div>
</asp:PlaceHolder>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btn_AddText" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
Would love some help on this.
Thanks.

You can use function pageLoad(sender, args) { } instead of $(document).ready()
Instead of targeting browser-specific optimizations, the ASP.NET AJAX pageLoad() shortcut function is called as a result of a more uniform process.
That process is queued up the same way on all browsers, via a call to setTimeout with a timeout of 0 milliseconds. This trick leverages JavaScript’s single-threaded execution model to (theoretically) push the init event back until just after the DOM has finished loading.
Counter-intuitively, this isn’t the only point at which pageLoad() is called. It is also called after every partial postback. It basically functions as a combination of Application.Init and PageRequestManager.EndRequest.
source: https://encosia.com/document-ready-and-pageload-are-not-the-same/

there are 2 events add_init and document.ready run same time ones per page refresh
and the second is pageload that triggered every async postback ,but not triggered on first load
I made 3 functions and put the code where its relevent
Sys.Application.add_init(function () {
// Initialization code here, meant to run once.
try {
//doOnes();
doAllways();
} catch (e) {
console.log('Sys.Application.add_init: ', e);
}
});
$(document).ready(function () {
console.log('document.ready');
//doAllways();
}
function pageLoad() {
console.log('pageLoad');
//doAftrerAjax();
doAllways();
}
function doOnes() {
console.log('doOnes');
}
function doAftrerAjax() {
console.log('doAftrerAjax');
}
function doAllways(){
console.log('doAllways');
AfterPostBack(); <---this the place for the code
}
`

Related

jQuery click not getting picked up

I have a input in a td and the click script is not getting picked up and im not sure why? There are no errors in the console.
<td>
<input type="button" class="edit btn btn-primary" value="Edit"></input>
</td>
Script below:
<script>
$('.edit').click(function () {
console.log("Test")
})
</script>
The 'click' even listener might run before the .edit element is rendered to the page. maybe wrap it in a document ready function to ensure it doesn't run till the page is loaded. like this:
$( document ).ready(function() {
$('.edit').click(function () {
console.log("Test")
})
});
Please add jquery.js file first like this.
<script src="https://code.jquery.com/jquery-3.6.0.js"> </script>
<script type="text/javascript">
$('.edit').click(function () {
console.log("Test")
})
</script>

body on load not works on jquery

I am trying to do this:
$(document).ready(function(){
$('body').on('load', function(){
alert('ok');
});
});
But it not works.
Any help?
First, you have to use $(window).load() instead of $('body').on('load'()).
Then, you need to separate the onload() function and the ready() function:
$(document).ready(function(){
alert("fires when webpage is loaded");
});
$(window).on('load', function(){
alert("fires once when window/body has been loaded");
});
Have a look at this to understand the difference.
Whay your code does not work?
When DOMContentLoaded event fires... That is the event that triggers the $(document).ready() callback. Obviouly, the body has already loaded too.
So if you wait for the whole DOM to be loaded to define an event handler about body loaded... It will never be executed because that event has fired before the event handler has been defined.
And after a close look: There is squarely no "load" event fired for the body... Whatever its content.
So as in pythan's answer, that should be $(window).on("load", function(){...})
See below:
console.log("before")
$("body").on("load", function () { // That NEVER fires.
console.log("Body loaded");
});
console.log("after")
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body>
<img src="https://via.placeholder.com/500" />
</body>
But anyway, you don't need both for sure.

Call Different domain in dialog window

I want to remove the dependency of Iframe from my application. What are the possible way I can call a different application URL other than using iframe, object or html embeded variable.
I am trying something like this.
<body>
<a class="ajax" href="http://www.google.com">
Open in Modal Window
</a>
<script type="text/javascript">
$(function (){
$('a.ajax').click(function() {
var url = this.href;
var dialog = $('<div style="display:none" class="Waiting"></div>').appendTo('body');
dialog.dialog({
close: function(event, ui) {
dialog.remove();
},
modal: true
});
dialog.load(
url,
{},
function (responseText, textStatus, XMLHttpRequest) {
dialog.removeClass('Waiting');
}
);
return false;
});
});
</script>
</body>
sorry can't comment
assuming there's no no anti CSF or similar measures on the target website you can use JavaScript ajax
or do it server side by grabbing the site
for better help describe what you're trying to archive what you did providing sample code if possible

Typeahead twitter bootstrap not functioning mvc4

I am trying to implement this code here
but I get no results while running the project.
here is my code:
View:
<input type="text" name="names" value="" id="typeahead" data-provide="typeahead" autocomplete="off" />
Again on view (index.cshtml):
<script type="text/javascript">
$(function () {
$('#typeahead').typeahead({
source: function (term, process) {
var url = '#Url.Content("~/index/GetNames")';
return $.getJSON(url, { term: term }, function (data) {
return process(data);
});
}
});
})
</script>
Controller (indexController.cs):
[HttpGet]
public JsonResult GetNames(string term)
{
// A list of names to mimic results from a database
List<string> nameList = new List<string>
{
"Jonathan", "Lisa", "Jordan", "Tyler", "Susan", "Brandon", "Clayton", "Elizabeth", "Jennifer", "Hadi"
};
var results = nameList.Where(n =>
n.StartsWith(term, StringComparison.OrdinalIgnoreCase));
return new JsonResult()
{
Data = results.ToArray(),
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
}
here are also the js scripts added at the end of the view page:
<script src="~/Scripts/bootstrap.js"></script>
<script src="~/Scripts/jquery-1.9.1.js"></script>
<script src="~/Scripts/jquery-1.9.1.intellisense.js"></script>
<script src="~/Scripts/jquery-1.9.1.min.js"></script>
here is also the list of bugs I get on runtime:
Uncaught TypeError: undefined is not a function bootstrap.js:29
Uncaught ReferenceError: intellisense is not defined jquery-1.9.1.intellisense.js:1
Uncaught TypeError: Object [object Object] has no method 'typeahead'
Please let me know what am i doing wrong!
This may be a bit old but you cannot use source: you must use either remote:, prefetch:, or local: as the error reads.
Which version of bootstrap are you using? The standalone plug-in no longer supports all of the different data providers in the same way as the old one did
First off, make sure you place the script tag referencing jquery first, before bootstrap. Also only load a single jquery script tag.
Like this:
<script src="~/Scripts/jquery-1.9.1.min.js"></script>
<script src="~/Scripts/bootstrap.js"></script>
All the JS libraries you are using at the moment have one dependancy: jQuery so they need to be loaded in right order:
<!-- jQuery should always be the first -->
<!-- jquery-1.9.1.js and jquery-1.9.1.min.js are the same but .min version is minified and always lighter, so use it for faster page load -->
<script src="~/Scripts/jquery-1.9.1.min.js"></script>
<!-- Any other library should be loaded after jQuery -->
<script src="~/Scripts/bootstrap.js"></script>
<script src="~/Scripts/jquery-1.9.1.intellisense.js"></script>
Hope this helps ;)

Webkit ignores onwebkitspeechchange callback

Using speech recognition available in Google Chrome version 11 and jquery
<input type="text" id="txtAns" x-webkit-speech="x-webkit-speech" onwebkitspeechchange="onChange()" />
I put the onChange() function in a js jquery file that I referenced in the head of my web page
After a successful speech recognition, the onChange function should be called but it is not. If I create a javascript function on the page called onChange() it is called. Any reasons why the jquery version is not being called?
I think you put the script on the head section of the dom. Try putting the script on the bottom, also you could try adding document ready if you want to include it on the head.
<input id="queryField" type="text" x-webkit-speech>
Javascript
<script>
var field = document.querySelector('#queryField');
field.onwebkitspeechchange = function(e) {
console.log("You have spoken");
};
</script>
jQuery
<script>
$('#queryField').bind('webkitspeechchange', function() {
console.log("You have spoken");
});
</script>