Passing a parameter to a popup in javascript [duplicate] - javascript

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
JavaScript query string
...Riight. I found the answer to my problem. I'd forgot to add '' around some jsp scriplet tags in my popup window code. Because of this values that should've been strings weren't handled properly.
I'll flag this question for moderator attention. The only thing one can learn from this question is to pay attention when using jsp scriptlets.
I want to open a popup from my .js code using window.open(). I have a couple of parameters that I need to pass to this popup. I'm surprised it's not as simple as I would've thought, I've tried searching for answers but all I found were solutions that were quite complicated - I hope there's a simple answer.
There's a multitude of ways I've tried doing this, but this is how I think it should work..
window.open('../common/MapPopup.jsp?current='+currentPosition+'&areas='+sAreas, 'mywindow', 'width=600,height=450,scrollbars=yes');
I'm at a total loss. Could it be that window.open just doesn't work in this situation?
edit: Currently my page behaves like this. The user presses a form button, which launches a query into our database. From this data a DataTables table is created. As the DataTable is being initialised, so is a piece of jQuery code. I'm using jQuery to open a popup when the user clicks on a row in the DataTables table.
renderReport: function(response){
$('#requestDataContainer').html(response);
oTable = $('#dataTable').dataTable({
"bPaginate": true,
--snip-- //DataTables init
});
var sProcedures = new Array();
var sAreas = new Array();
var sCurrentPosition = null;
$('#dataTable tr').click(function(){
var sCurrentPosition = oTable.fnGetData(this,9);
if(sCurrentPosition!=null){
$('#dataTable').find('tr').each(function(){
var foo = oTable.fnGetData(this);
if(foo!=null){
if(foo[8]!='null')
sAreas.push(foo[8]);
if(foo[7]!='null')
sProcedures.push(foo[7]);
}
});
}
window.open('../common/reportMapPopup.jsp?procedures='+sProcedures+'&areas='+sAreas+'&current='+sCurrentPosition, 'reportMap', 'width=600,height=450,scrollbars=yes');
});
},
I have to admit, I'm quite newbish when it comes to webcode. I'm kind of learning on the go, so the terminology might be new to me and I could be doing things in a really silly way. It could be that I found the answer when I was looking for it on the web, but just didn't realise it.

If the query string in your url gets passed to the window you should be able to access those parameters from the window using JS using the location object(http://www.w3schools.com/jsref/obj_location.asp)
If you have access to the actual page - can you not retieve the params using jsp?

Related

how can i create a reg exp or array to change this?

ok guys i have been working on these, this is like my main project and im pretty anxious about it, i have been practicing but im still a newbie. My code is like this and it does what it has to do, but im thinking that it could be improved to be better and more reusable, sorry to spam if i am, i already asked on the spanish version of the website with no satisfactory answer, im new to web developing and to this site, i always read the content on this site to answer my questions but for this time i didnt know how to exactly use the previous answers to fix my code, since im new to web developing and im trying to use jquery bit by bit. As i said my question is how can i create an array or a reg exp that does all the things this code does? without having to use .replace function all those times
i have tried urlencode function, and tried to iterate over arrays on jquery but i still dont know how to do it properly.
$( ".linkbestia" ).each(function() {
lnk = $(this).text();
enlace= $(this).attr("href");
espacios=lnk.replace(" ","_");
maslimpio=espacios.replace("'","%27");
muchomaslimpio=maslimpio.replace("(","%28");
muchomuchomaslimpio=maslimpio.replace(")","%29");
nuevoenlace=$(this).attr("href",enlace+muchomuchomaslimpio);
});
the actual output is for example codedquote'replaced space as i said it already does what it has to do, but i know it can be improved, i hope you guys help me since in my country these kind of questions cant be answered without a ton of difficulties
what it does right now:
what the user writes would look like this
the result would look like this
If I understand correctly you want to take href from below:
<a class="linknpc" href="url/in/url/url/The%27White_Mob">
and expected output is
The'White_Mob
after you get the href and lets say the var enlace looks like below.
var enlace = "url/in/url/url/The%27White_Mob"
Below will first use String split on '/' to get all sections from href and from resultant array get the last element by pop() and use decodeURIComponent to decode the encoded uri.
var ans = decodeURIComponent(enlace.split('/').pop())
ans would now have the value: The'White_Mob
note: If the href ends with '/' then you need to adjust above solution accordingly

JS variable inside of PHP inside of JS inside of PHP [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 4 years ago.
Having a bad day. This one has stumped me all morning. All the solutions I've found have stopped one step short of where I need to go.
I have a legacy PHP/JS app that I'm working on. Rather than trying to explain it, I'll just show what I need to do.
<?php
$phpDate_1 = new Date($someDate);
$phpDate_2 = new Date($someOtherDate);
//...There are a bunch of these
$phpDate_n = new Date($endOfTime);
<script language="javascript">
function myFunction() {
var line = aUserSelection; //an int from user which tells me what date to use
//Next line is the problem. I'm trying to pull the month from the appropriate PHP date into the JS variable.
var theMonth = "<?php echo $phpDate_" + line + "->getMonth();?>";
}
</script>
?>
I must have tried 20-30 combinations of single and double quotes, escapes, dots, pluses, and so on, but I keep getting errors over the "line" part. Unexpected character, encapsed strings, etc.
Hoping someone can point me in the right direction because my brain is fried at this point. Answers in pure JS and PHP only please because that's how the app is built. Thanks.
You need to close your php (?>) before outputting the javascript to fix the syntax error that you got.
However, with that said, you are trying to incorporate the javascript line variable into the variable name for $phpDate, to generate something like $phpDate_1.
If you don't want to go with an AJAX solution, your best bet would be to output each line's date into a javascript array. This is strongly discouraged, but if this is a legacy application that you cannot make many changes to, this might be your only option.

Can't access object in jquery (prevObject?)

A function in my WP plugin has just randomly (as far as I can tell) stopped working.
Here's the code in question:
window.send_to_editor = function(html) {
var classes = jQuery('img',html).attr('class');
var items = classes.split(" ");
... more stuff here
}
I've confirmed that the html variable is indeed an img html tag. Here's what firebug shows when I do a console.log of the object (console.log(jQuery('img',html));):
Object[]
context -> undefined
jquery -> "1.11.2"
length -> 0
prevObject -> Object[img.alignnone.size-full.wp-image-1234 name.jpg]
And the error it shows is classes is undefined.
I figure there's something wrong with the object I get, but this used to work recently and I'm not aware of any changes in the site that could have caused this.
I'd appreciate any input on this.
EDIT:
More info. This happens with two plugins which are supposed to be unrelated (made by different people). It happens when, after uploading an image to the server (or selecting a previously uploaded picture) you try to insert it into the post.
As I said before this error has appeared out of nowhere, it was working as intended a couple days ago. The only thing I can think of that has changed since then is the domain name, but I can't see how that could be related.
The jQuery selector always returns a jQuery object, but when the length is 0 then no elements were found matching the selector that you provided. In your example you've confirmed that nothing is selected as the length of the jQuery object is 0. Perform a check whether an element was selected like this:
var $els = jQuery('img',html),
classes;
if ($els.length) {
classes = $els.attr("class");
}
Keep in mind that your DOM query is limited by what you pass in as the html parameter. If you simply want to find the images on the page do: var $els = jQuery('img');
I finally managed to fix this; the key was parsing the html string variable into proper HTML, using jQuery.parseHTML(). Thanks to everyone who helped!

Creating quiz with one page that determines outcome (no access to database or php, needs to be in javascript/html/etc.)

Note - I do not have access to a database and cannot use PHP, thus is why I'm asking for help. My hands are tied and I can only do through html/javascript/etc.
I'm creating a online quiz that has 10 questions (1 question per page), but only one question determines the outcome to 4 different final locations. Question 3 (page 3) is the determining question, but I need to "record" the radio button selection, so the last question (page 10) determines the URL for the submit button, which in hand redirects the user.
All the other questions are vanity questions, as question 3 is the big determining factor.
I was thinking that the answer on question 3 (page 3) creates a cookie with 1 of 4 values (based off of the radio button selection), then on the last question (page 10) the cookie is read and the value determines the url, which then writes in the URL redirect code for the submit button. This is just theory now, as I'm not sure if this is doable.
Any advice would be greatly appreciated.
I can see at least 2 more ways to do it:
Use an iFrame:
Keep a hidden input outside an iFrame. This input records the answers. An iFrame script must append the answer (comma-separated or whatever) to the hidden input in the parent when a question is submitted. A new page gets loaded into the iFrame after that. When the last question is encountered, the iFrame script can update and read the parent input, then determine which url to load, and then load the url into the parent document location.
Use jQuery Ajax: EDITED to remove hidden input which isn't needed here:
Using jQuery Ajax you can load the next question into a main div, and also retain the values of the answers you want in the jQuery script.
EDIT: Here is another way, using CSS and javascript/jQuery:
Load all the questions into ONE page. But only the current question has style='display:block;', the others must have style='display:none'. When a question is answered, use javascript or jQuery to record the answers in a variable. In your case it seems you are only interested in one answer, so it's even easier. Then use your script to change the display attributes depending on which question should show. Finally, if it is the last question which is submitted, you can determine the url and load the next page.
HOW TO GET THE PARAMETER FROM A QUERY STRING:
function getQuery(key_str) {
// return value of key_str variables query string of url
// Example: url = "index.html?answer=5"; if key_str = "answer" then it returns "5"
if(window.location.search) {
var query = window.location.search.substr(1);
var pairs = query.split("&");
for(var i = 0; i < pairs.length; i++) {
var pair = pairs[i].split("=");
if(unescape(pair[0]) == key_str) return unescape(pair[1]);
}
return null;
}
}
function go() {
var answer = getQuery(answer);
var durl = "http://www.defaulturl.com";
switch (answer) {
case '1' : url = "http://www.url1.com"; break;
case '2' : url = "http://www.url2.com"; break;
case '3' : url = "http://www.url3.com"; break;
default : url = durl;
}
window.location.href = url;
return false;
}
<input type="button" value="Submit" name="butt" onclick="return go();">
Why don't you pass the relevant answer as a GET ? You can easily read the query string in Javascript.
Your cookie method would work fine. Also doable with HTML5 local storage. But I have to ask... if you're not recording the output of the other 9 questions anywhere, what's the point of asking them? :)
I would do everything I could to avoid cookies and just keep the quiz on one page... I know you're saying you don't have backend access, but I just wanted to throw that out there.
You can make the user feel like it's really 10 "pages," but your JS would be swapping "pages" and storing the form values in one page load. Think about a DOM tabbed element with 10 tabs.
Totally agree with Alex that HTML5 local storage would work, too.

how to get html from CKEditor? [duplicate]

This question already has answers here:
Get formatted HTML from CKEditor
(12 answers)
Closed 9 years ago.
I'm using CKEditor in my web app, but I don't know how to get html content from it.http://cksource.com/ckeditor
I searched online found one said using getData() method, but there is no getData() method after typing dot after the controler. Can anyone give me a sample code to get html from CKEditor controller? Thank you in advance.
To get htmlData from editor you should use the code snippet bellow:
var htmldata = CKEDITOR.instances.Editor.document.getBody().getHtml();
If this solution won't work, check if you have BBCode plugins installed.
getData() is part of the javascript API.
It seems that you are trying to do it at the server side, so you should check the specific API of whatever wrapper you are using, or just check the value in the form posted data.
Not sure how you're implementing usage of the CKEditor.
If you're replacing a textarea using CKEDITOR.replace( 'NameOfTextarea', this should work:
CKEDITOR.instances.NameOfTextarea.on( 'instanceReady', function( instanceReadyEventObj )
{
var editorInstanceData = CKEDITOR.instances.NameOfTextarea.getData();
alert( editorInstanceData );
});
Replace "NameOfTextarea" with the name of your textarea, it's used to name the editor instance.
It's a good idea to put it inside the "on instanceReady" function so you don't get an undefined error.
Joe

Categories