|
How I can implement a custom JavaScript type "mydictionary" which acts like a dictionary via its "put" and "get" methods and also has "getItems" which returns a list of key-value pairs in the order in which the keys were originally added ?
|
|
|
|
|
I am trying to convert this code to use private variable inside a closure:
var cache = {};
function cachedAjax(url) {
if (cache.hasOwnProperty(url)) {
return cache[url];
}
var data = $.ajax({
url: url,
async: false
}).responseText;
cache[url] = data;
return data;
}
so I wrote it like this:
var cachedAjax = (function (url) {
var cache = {};
if (cache.hasOwnProperty(url)) {
return cache[url];
}
var data = $.ajax({
url: url,
async: false
}).responseText;
cache[url] = data;
return data;
})();
Is this correct ?
|
|
|
|
|
No. You're expecting the url parameter to be passed to the wrapper function, and you're not returning a function.
Try this instead:
var cachedAjax = (function(){
var cache = {};
return function(url){
if (cache.hasOwnProperty(url)) {
return cache[url];
}
var data = $.ajax({
url: url,
async: false
}).responseText;
cache[url] = data;
return data;
};
})();
However, you should avoid synchronous AJAX requests, as they will block the UI for the duration of the request. You should try to rewrite your code using callbacks or promises[^] instead.
var cachedAjax = (function(){
var cache = {};
return function(url){
if (cache.hasOwnProperty(url)) {
return cache[url];
}
var promise = $.ajax({ url: url });
cache[url] = promise;
return promise;
};
})();
cachedAjax("someUrl").then(function(data){
});
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
|
I want to create an HTML page that contains a button which would open a file in Word 2010 (using JavaScript and ActiveX) then automatically print it (the button says "Print"). I've been able to get the file to open in Word, but I'm having difficulty getting it to print the file without making the user do it themselves. Here is the function I'm calling from the button:
function printWord() {
var objWord;
var url="ReleaseNotes.docx";
objWord = new ActiveXObject("Word.Application");
objWord.Visible = true;
objWord.Documents.Open(url);
objWord.Documents.Print();
}
The function is opening the document as it should, but the Print function isn't working. I'm not even getting the Print dialog to appear. Another item related to this is that I'd like to be able to have it print in Landscape view vs. portrait. Any help would be greatly appreciated.
Chris
|
|
|
|
|
This code will only work in Internet Explorer on Windows, and only if the user has Microsoft Office installed, and only if the user chooses to allow your site to "Initialize and script ActiveX controls not marked as safe for scripting".
Unless you're developing an Intranet site, where you can control the browser your users are using, it's highly unlikely that this code will ever run.
Christopher Cote wrote: objWord.Documents.Print();
There is no Print method on the Documents collection[^]. You're looking for the PrintOut method on the individual document[^], or the application[^].
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I have a from named CAT_ADD in which the user can add any number of categories. A table contains category selection (Option list), No. of hours (Input box) & a question Do you want to add more categories? yes/ no . (Radio buttons). If the user click 'Yes' button a Javascript function Hello() is called. It add a new row with option list filled with PHP query CAT_fill, no. of hours with PHP code Hour_fill & Ans_fill. When the user Clicks NO button the form is submitted and calls a PHP file called PROCESS.PHP.
My problem is that the values of the controls created dyanamically are not posted or not available in the PROCESS.PHP. Please help. Here is the code section.
<script type="text/javascript">
function hello(p)
{
var table = document.getElementById("myTable");
var x = document.getElementById("myTable").rows.length;
var y = document.getElementById("myTable").rows[x-1].cells;
y[2].innerHTML = "*********";
var row = table.insertRow(-1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
++x;
var y = document.getElementById("myTable").rows[x-1].cells;
var z = "<?php echo cat_name_fill();?>";
y[0].innerHTML = z;
y[1].innerHTML = "<?php echo hour_fill();?>";
y[2].innerHTML = "<?php echo ans_fill();?>" ;
document.getElementById("myTable").rows[x-1].appendChild(row);
}
function cat_name_fill()
{
$f_name=md5($_GET['usr_name']);
$con = mysqli_connect("localhost","root");
mysqli_select_db($con,"admin");
$query = "Select CAT from " . $f_name ;
$result= (mysqli_query($con,$query));
$row=mysqli_fetch_row($result);
$query = "SELECT * FROM quest_category";
$result = mysqli_query($con,$query);
echo "<select name='CAT[]'>";
while($row=mysqli_fetch_array($result, MYSQLI_ASSOC)){
echo "<option value='".$row['CODE']."'>".$row['SUBJECT']." </option>";
}
echo "</select>";
}
function hour_fill()
{
echo "<input type='text' name='h[]' style='text-align:center' size='20' value='0'>";
function ans_fill()
{
echo "<input type='radio' value='N' onclick ='f_submit()' checked> No ";
echo "<input type='radio' value='Y' onclick = 'hello()'> Yes";
}
function f_submit()
{
var r = confirm("Submit the form?");
if (r == true) {
document.getElementById('cat_add').submit();
}
|
|
|
|
|
Don't know PHP but the radio boxes have no "name" so can't be read via your form submission, and your textboxes all have a name "h[]" so when you request "h[]" from the form you'll get all the values comma separated.
Rather than creating the components are you are (innerHTML) you're probably better using jQuery to create the components for you, and make sure they are added inside the form element.
|
|
|
|
|
<div class="arrow-up"></div>
<div class="list-elements">
<div class="list-inner">
<div class="elem1">elem1</div>
<div class="elem1">elem1</div>
<div class="elem1">elem1</div>
</div>
</div>
<div class="arrow-down"></div>
.list-elements {
overflow: hidden;
position: relative;
height: 520px;
}
.list-inner {
position: absolute;
top: 0px;
}
On website we have some icon. On hover this icon: div with elements of list are visible. But when list has more than four elements other elements don't display, because we use overflow-hidden. If we want to see next elements of list, we must click on 'icon arrow-down' and next elements will display on screen (position absolute is change on click)
It's clear?
|
|
|
|
|
Do you have a question?
There are two kinds of people in the world: those who can extrapolate from incomplete data.
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
LOL, sorry I forgot about question.
So, when list will scroll and last element of list will show in viewport I want to hide 'down-arrow'. It's my question. How can I recognize this last element of list in viewport?
|
|
|
|
|
|
hello every one ...Any one say me about that how data-set is used in java ...
My project is about to recognize different English letters and digits also
and i have complete data set also but i don't know how we used or integrate or implement in java to recognize and check as a test please help if any one know about ???
|
|
|
|
|
This is the Javascript forum. Please us the Java forum, but also add some proper detail to your question. As it stands it is difficult to understand what your problem is.
|
|
|
|
|
I am stuck with this and need help.
I am trying to display a PDF in IFRAME using base64 string as input with URL.createObjectURL.
I got some idea from this link: Creating a Blob from a base64 string in JavaScript
But still I am only able to process images with the code and not PDF, donot know what is wrong.
Code: as I have base64 in the js itselt so uploading the code to this location
Download the full file
|
|
|
|
|
Post the relevant code and explain clearly what you need from us.
There are two kinds of people in the world: those who can extrapolate from incomplete data.
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
Can someone please suggest a different, more efficient approach to the code below?
It works in terms of successfully posting data to the database (phew - too much head banging).
However, it is awfully, inefficient and annoying when you receive a million alert messages saying "Data Added Successfully"
Is there a way to just make it one click to submit all the records?
I tried an idea that did not work.
First here is the code that works with those many alert messages.
function getAllEmpData() {
var data = [];
$('tr.data-contact-personm').each(function () {
var ename = $(this).find('.employeename01').val();
var etitle = $(this).find('.employeetitle01').val();
var email = $(this).find('.employeeemail01').val();
var alldata = {
'emplName': ename,
'emplTitle': etitle,
'empMail': email
}
data.push(alldata);
});
console.log(data);
return data;
}
function getAllSourcepData() {
var data = [];
$('tr.data-contact-person').each(function () {
var sname = $(this).find('.sourcename01').val();
var saddress = $(this).find('.sourceaddress01').val();
var sincome = $(this).find('.sourceincome01').val();
var alldata = {
'mySource': sname,
'mySAddress': saddress,
'mySIncome': sincome
}
data.push(alldata);
});
console.log(data);
return data;
}
function getAllSpouseData() {
var data = [];
$('tr.data-contact-person2').each(function () {
var spname = $(this).find('.spousename01').val();
var spaddress = $(this).find('.spouseaddress01').val();
var spincome = $(this).find('.spouseincome01').val();
var alldata = {
'mySpouse': spname,
'mySPAddress': spaddress,
'mySPIncome': spincome
}
data.push(alldata);
});
console.log(data);
return data;
}
function getAllDividentData() {
var data = [];
$('tr.data-contact-person3').each(function () {
var divname = $(this).find('.dividentname01').val();
var divaddress = $(this).find('.dividentaddress01').val();
var divincome = $(this).find('.dividentincome01').val();
var alldata = {
'myDivName': divname,
'myDivAddress': divaddress,
'myDivIncome': divincome
}
data.push(alldata);
});
console.log(data);
return data;
}
function getAllReimbursedData() {
var data = [];
$('tr.data-contact-person4').each(function () {
var reimname = $(this).find('.reimbursmentname01').val();
var reimaddress = $(this).find('.reimbursmentaddress01').val();
var reimincome = $(this).find('.reimbursmentincome01').val();
var alldata = {
'myReimbursName': reimname,
'myReimbursAddress': reimaddress,
'myReimbursIncome': reimincome
}
data.push(alldata);
});
console.log(data);
return data;
}
function getAllHonorariaData() {
var data = [];
$('tr.data-contact-person5').each(function () {
var honorname = $(this).find('.inputHonoraria01').val();
var alldata = {
'myHonorname': honorname
}
data.push(alldata);
});
console.log(data);
return data;
}
function getAllGiftData() {
var data = [];
$('tr.data-contact-person6').each(function () {
var gifname = $(this).find('.giftname01').val();
var gifaddress = $(this).find('.giftaddress01').val();
var gifincome = $(this).find('.giftincome01').val();
var alldata = {
'myGiftname': gifname,
'myGiftaddress': gifaddress,
'myGiftincome': gifincome
}
data.push(alldata);
});
console.log(data);
return data;
}
function getAllOrgData() {
var data = [];
$('tr.data-contact-person7').each(function () {
var orgsname = $(this).find('.orgname01').val();
var orgsaddress = $(this).find('.orgaddress01').val();
var orgsincome = $(this).find('.orgincome01').val();
var alldata = {
'myOrgname': orgsname,
'myOrgaddress': orgsaddress,
'myOrgincome': orgsincome
}
data.push(alldata);
});
console.log(data);
return data;
}
function getAllCreditorData() {
var data = [];
$('tr.data-contact-person8').each(function () {
var creditname = $(this).find('.creditorname01').val();
var creditaddress = $(this).find('.creditoraddress01').val();
var creditincome = $(this).find('.creditorincome01').val();
var alldata = {
'myCreditorname': creditname,
'myCreditoraddress': creditaddress,
'myCreditorincome': creditincome
}
data.push(alldata);
});
console.log(data);
return data;
}
$("#btnSubmit").click(function () {
var data = JSON.stringify(getAllEmpData());
console.log(data);
$.ajax({
url: 'closures.aspx/SaveEmpData',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({ 'empdata': data }),
success: function () {
alert("Data Added Successfully");
},
error: function (xhr, status, error) {
alert(xhr.responseText);
}
});
});
$("#btnSubmit").click(function () {
var data = JSON.stringify(getAllSourcepData());
console.log(data);
$.ajax({
url: 'closures.aspx/SaveSourceData',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({ 'empdata': data }),
success: function () {
alert("Data Added Successfully");
},
error: function (xhr, status, error) {
alert(xhr.responseText);
}
});
});
$("#btnSubmit").click(function () {
var data = JSON.stringify(getAllSpouseData());
console.log(data);
$.ajax({
url: 'closures.aspx/SaveSpousData',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({ 'empdata': data }),
success: function () {
alert("Data Added Successfully");
},
error: function (xhr, status, error) {
alert(xhr.responseText);
}
});
});
$("#btnSubmit").click(function () {
var data = JSON.stringify(getAllDividentData());
console.log(data);
$.ajax({
url: 'closures.aspx/SaveDividentData',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({ 'empdata': data }),
success: function () {
alert("Data Added Successfully");
},
error: function (xhr, status, error) {
alert(xhr.responseText);
}
});
});
$("#btnSubmit").click(function () {
var data = JSON.stringify(getAllReimbursedData());
console.log(data);
$.ajax({
url: 'closures.aspx/SaveReimbursedData',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({ 'empdata': data }),
success: function () {
alert("Data Added Successfully");
},
error: function (xhr, status, error) {
alert(xhr.responseText);
}
});
});
$("#btnSubmit").click(function () {
var data = JSON.stringify(getAllHonorariaData());
console.log(data);
$.ajax({
url: 'closures.aspx/SaveHonorariaData',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({ 'empdata': data }),
success: function () {
alert("Data Added Successfully");
},
error: function (xhr, status, error) {
alert(xhr.responseText);
}
});
});
$("#btnSubmit").click(function () {
var data = JSON.stringify(getAllGiftData());
console.log(data);
$.ajax({
url: 'closures.aspx/SaveGiftData',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({ 'empdata': data }),
success: function () {
alert("Data Added Successfully");
},
error: function (xhr, status, error) {
alert(xhr.responseText);
}
});
});
$("#btnSubmit").click(function () {
var data = JSON.stringify(getAllOrgData());
console.log(data);
$.ajax({
url: 'closures.aspx/SaveOrgData',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({ 'empdata': data }),
success: function () {
alert("Data Added Successfully");
},
error: function (xhr, status, error) {
alert(xhr.responseText);
}
});
});
$("#btnSubmit").click(function () {
var data = JSON.stringify(getAllCreditorData());
console.log(data);
$.ajax({
url: 'closures.aspx/SaveCreditorData',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({ 'empdata': data }),
success: function () {
alert("Data Added Successfully");
},
error: function (xhr, status, error) {
alert(xhr.responseText);
}
});
});
});
});
});
});
});
});
});
});
});
</script>
|
|
|
|
|
This is kinda rough. Do you have control over the web endpoint?
The biggest issue that I'm seeing is that you don't really have a plan for how the client and server sides will communicate in a consistent manner. It looks like you're likely building a dynamic page (with ASP.NET, JSP, PHP, whatever) and parsing it into JS objects, then sending those objects back up via AJAX. That's leveraging 2 totally different approaches (HTML Forms vs AJAX) to handle each leg of the model change pipeline.
The lack of a unified pipeline is why the JS is bulky, difficult, and not at all DRY. It will also make further development much more difficult as you try and keep track of how processing is handled for read vs update.
So my initial advice would be to pick a pipeline, and use it consistently. Regardless of which one you pick your code will clean up quickly.
"There are three kinds of lies: lies, damned lies and statistics."
- Benjamin Disraeli
|
|
|
|
|
I am actually building a dynamic page with javascript and parsing to asp.net.
Specifically, I used javascript to give users the ability to use several Add More buttons to add more rows if needed when completing a survey.
This is really unique as my search and research as to whether it has been before and I see no evidence of this being done before.
So, this created a situation where I am using ajax to make calls to several asp.net webmethods and with the webmethods, are able to post data to the database.
It is very convoluted at least from my standpoint.
I have managed to make it a bit more efficient than the code I posted.
It has several phases and I am finding ways to resolve each phase and move on to the next phase.
As a matter of fact, I only have two faces and I will be done.
One, is to add a loading message so user will know something is happening.
The last phase is to reload page after each submit so users won't submit duplicate entries.
So, I am getting there.
|
|
|
|
|
You do not want to keep wiring up the button click event over and over and over. It makes for hard to read code.
Use your own callback function instead of the built-in success one and then you can remove the alert and keep a counter or a set of variables that you can check to see when they are all done and then give one single message.
There are two kinds of people in the world: those who can extrapolate from incomplete data.
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
Here's my JS
@section scripts
{
<script type="text/javascript">
$(function () {
console.log("HELLO WORLD");
$.connection.hub.url = 'myip:8094/signalr';
var proxy = $.connection.DashboardHub;
proxy.client.NotifyAllClientsOfChanges = function () {
var searchUrl = "Home/GetData";
$.ajax({
url: searchUrl,
type: "POST",
success: function (data) {
$("#divData").html(data);
}
});
};
$.connection.hub.start({ transport: 'auto', xdomain: true })
.done(function () {
console.log('Connected.');
})
.fail(function (e) {
console.log('Unable to connect:' + e);
});
});
</script>
}
I see the "HELLO WORLD" console log at the top, yet I don't see either of the Console logs for "Connected" or "Unable to connect"
I see an error in the console that says "Cannot read property 'client' of undefined"
I've followed many Google/CP/SO posts on similar topics and tried many things. I'm not entirely sure that I have this coded correctly. This is really getting frustrating now.
Anyone see what's wrong?
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Looking at this tutorial[^], it looks like the Javascript hub name is camel-cased.
So if your C# code has a hub called DashboardHub , your Javascript code will refer to it as $.connection.dashboardHub (with a lowercase "d").
If it still doesn't work, try adding a console.dir($.connection); line at the start of your function to see what's defined.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I had changed the case AFTER I posted this. No change.
However, adding "console.dir($.connection);" got me this "function signalR(url, qs, logging)"
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Have you included the script reference for the auto-generated SignalR script?
<script src="~/signalr/hubs"></script>
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Yes, above the script I posted I have
<script src="/Scripts/jquery-3.1.1.min.js"></script>
<script src="/Scripts/jquery.signalR-2.1.2.js"></script>
<script src="/signalr/hubs"></script>
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|