KCfinder, how to pass or send the url variable? - javascript

Im sorry if the title is seem confusing :(
I Using KC Finder to develop my CMS file manager
i have HTML code like this
<input type="text" name="url-1" value="" />
<button class="button-primary" onclick="openKCFinder_singleFile(); return false;">Insert</button>
here is the content of openKCFinder_singleFile()
function openKCFinder_singleFile() {
window.KCFinder = {};
window.KCFinder.callBack = function(url) {
window.KCFinder = null;
console.log(url) // URL is the call back result from KCfinder, example: /image/a.jpg
};
window.open('/wp-content/plugins/kcfinder/browse.php', 'kcfinder_single');
}
My Question is, how to set the value input with the value i get from kcfinder, as you can see we get the value from variable url, how to pass that to set as input value?
if the code in jQuery script, i really appreciate it :)
Many Thanks
GusDe...

Using jQuery, you can simply add :
$('#url-1').val(url);
See this.

I agree with the answer of user284291, but there is a but :) if you are using this function in a page which does not load jquery, you can send the url by replacing
$('#url-1').val(url);
with
var x = document.getelementbyid('url-1'); x.setAttribute('value', url);

Related

javascript add string to variable error

I have a JavaScript program that worked until I tried to change this: "foldername" to this: http://hokuco.com/test/"+"foldername"+"/index.html".
what is wrong with my code?
For anyone interested entire JS:
document.getElementById("submit").addEventListener("click", function(){
var url = document.getElementById("http://hokuco.com/test/"+"foldername"+"/index.html").value;
window.location.href = "url";
});
<input type id="foldername"></input>
<input type ="button" id ="submit/>
You probably meant:
document.getElementById("submit").addEventListener("click", function(){
var url = "http://hokuco.com/test/" + document.getElementById("foldername").value + "/index.html";
window.location.href = url;
});
Changes:
The parameter in the getElementById function is the same as the id attribute on the input element with the id "foldername".
The window.location.href should be set to a variable, not a quoted string.
More legibly, you would want:
document.getElementById("submit").addEventListener("click", function(){
var folder = document.getElementById("foldername").value;
var url = "http://hokuco.com/test/" + folder + "/index.html";
window.location.href = url;
});
Now, hopefully, it is much more clear about what's going on.
Please read the following documentation to better understand document.getElementById: https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById
Your code will most likely return an uncaught type error when trying to access a property of null. You must only pass a string referring to an Element object's ID.
What are you trying to accomplish? It looks like you are trying to redirect but are using a string literal instead of a variable.

Pass javascript variable to external php file

Yes, there are lots of questions to similar stuff but I can't figure it out, sorry.
I have a file with some javascript variables, depending on user input (but no form) and a normal HTML link to my php file.
<script>
function doStuff() {
var a = 'foo';
var b = 'bar';
window.location = 'newfile.php?a=' + a + '&b=' + b;
}
</script>
go to new php file
That works fine, I can access the data in newfile.php with $_GET.
newfile.php:
<?php
$a= $_GET['a'];
$b= $_GET['b'];
echo($a,$b); // works
?>
But I'd like to use POST. I guess I have to use ajax for that but how exactly?
jQuery is included btw so I could use $.ajax()
Any help is highly appreciated :)
EDIT:
Thanks for the quick response guys!
The JSON parsing doesn't work, I can't really figure out why - after clicking on the button the browser window disappears for a split second and I'm on the same page again which is unresponsive now :(
I went with the following code:
jQuery.post('newfile.php',{'a': a, 'b': b}); //curious: with or without ''?
setTimeout(function () {
window.location = 'newfile.php';
}, 5000); //this will redirct to somefile.php after 5 seconds
newfile.php:
$a= $_POST['a'];
$b= $_POST['b'];
echo('Testing: '.$a);
Right after clicking I can see the correct output in Firebug (Testing: foo) but of course after redirecting to the site the values are lost and I'm left with "Testing: "
What can I do?
You can use ajax to achieve this. Following is the code which works on a button click or anchor click.
HTML
<button type="button" id="button1">Click me </button>
Ajax
$('#button1').click(function() {
var a = $('#IDofYourFormelement').val();
var b = $('#IDofYourFormSecondElement').val();
$.post('/somefile.php', {'somevariable': a, 'variableB': b}, function(data) {
var parsed = JSON.parse(data);
if (parsed == 'success') {
setTimeout(function () {
window.location = '/somefile.php';
}, 3000);//this will redirct to somefile.php after 3 seconds
}
else
{
alert ('Invalid details');
}
});
});
and then in your somefile.php you can access it as
$a = $_POST['somevariable'];
$b = $_POST['variableB'];
//do some stuff and return json_encoded success/failure message
You can use the new with HTML5 FormData();
Code snippet from https://developer.mozilla.org/en-US/docs/Web/Guide/Using_FormData_Objects see also https://developer.mozilla.org/en/docs/Web/API/FormData and http://caniuse.com/#feat=xhr2 for browser support
var formData = new FormData();
formData.append("username", "Groucho");
formData.append("accountnum", 123456); // number 123456 is immediately converted to string "123456"
var request = new XMLHttpRequest();
request.open("POST", "http://foo.com/submitform.php");
request.send(formData);
I guess you are trying to post the variables using javascript and display the page post executing your post variables. Found a similar question and an answer in here - JavaScript post request like a form submit.
EDIT
The window.location will call another instance of you page and then will assign or replace the current doc, hence your previous post parameters are lost. If you want the page with your post parameters passed you need to do a form submit to your php page with method=POST also with the post parameters. That's what is written in the above stackoverflow link I shared.

Retrieve URL Parameters Sent Through JQuery .load()

I am not sure if what I'm trying to do is possible or if I'm going about this the right way. In some circumstances I want them to have a GET parameter as part of the URL. I want the receiving page to be able to differentiate whether the sending load has a parameter or not and adjust accordingly.
Here is what I have that is sending the load:
$(document).ready(function () {
$("a").click(function () {
$("div.pageContent").html('');
$("div.pageContent").load($(this).attr('href'));
return false;
});
});
In this case, the load could have "example.php" or "example.php?key=value". In looking around (primarily on this site), I've found things that seem to be close, but don't quite get there. In the page that is getting loaded (example.php), I have the following:
function $_GET(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.href);
if (results == null)
return "";
else
return results[1];
}
$(document).ready(function () {
var URL = "example2.php";
if ($_GET('key'))
{
URL = "example2.php?key=" + $_GET('key');
URL = URL.split(' ').join('%20');
}
$("div.output").load(URL);
});
If the sending source includes a query string, I want to add that to the URL and load it in a div that is unique to this page, otherwise I want to just load it as is without the query string. The big issue I'm running into (I believe) is since this is coming from an AJAX call, the "window.location.href" is not what was sent from the JQuery but rather the URL of the root page which never changes. Is there a way to be able to know what the full URL is that was sent from the load() in the first page by the second one?
Thank you in advance for your help.
I realized that the GET parameters were getting passed as I could access them through php without issue. I didn't know that I could insert php code into a javascript block but once I tried it, all worked out. My new code looks like this:
$(document).ready(function () {
var URL = "example2.php";
var myValue = "<?php echo $_GET['key']; ?>";
if (myValue !== "")
{
URL = "example2.php?key=" + myValue;
URL = URL.split(' ').join('%20');
}
$("div.output").load(URL);
});
I was able to get rid of the GET function out of javascript entirely. I probably made this much more difficult from the start but hopefully it can help someone else in the future.

how to remove a part of a url from a url using jquery?

I have a javascript variable
var url = http://www.abc.it/it/security/security.aspx?security=pdfcompare&../securitycomparepdf/default.aspx?SecurityTokenList=blab]2]0]blab$ALL|blab]2]0]blab$ALL|blab]2]0]blab$ALL;
I need to remove the part "../securitycomparepdf/default.aspx?" from it using jquery, so that the variable becomes
http://www.abc.it/it/security/security.aspx?security=pdfcompare&SecurityTokenList=blab]2]0]blab$ALL|blab]2]0]blab$ALL|blab]2]0]blab$ALL;
Can someone please suggest?
Thanks
Made a demo for you: http://jsfiddle.net/B3Uwj/27/ (from #tats_innit)
url = url.replace("../securitycomparepdf/default.aspx?", "");
Use the following JS
var url = "http://www.abc.it/it/security/security.aspx?security=pdfcompare&../securitycomparepdf/default.aspx?SecurityTokenList=blab]2]0]blab$ALL|blab]2]0]blab$ALL|blab]2]0]blab$ALL";
url = url.replace("../securitycomparepdf/default.aspx?","");
​alert(url);​
See a live example here

Get link to render as html rather than text json

I am responding to clicks on li's by using $.post to post to an action method in my MVC application.
I want to send a link back in Json.
Can I have this link render as html rather than text ? how ?
I tried this, just to test the html:
var link = "<b>Hi</b>";
var encoded = Server.HtmlEncode(link);
that came out as <b>Hi</b>
Surely there is just a Json.encode or visual studio method I can use and I don't have to format it myself? Have googled fairly extensively and can't find anything about an Json.encode
var link = "<b>Hi</b>";
var encoded = new JavaScriptSerializer().Serialize(link);
the page rendered "\u003cb\u003eHi\u003c/b\u003e"
If I send just the link variable, i.e:
var link = "<b>Hi</b>"
<b>Hi</b> renders
This is the line which sends it back:
return Json(new {Title = pTitle, Selection = pSelection, Link = pLink}, JsonRequestBehavior.AllowGet);
Starting to get frustrated, wtf!
Silly me, I didn't post enough code where the problem was:
<script type="text/javascript">
function TreeView_onSelect(e) {
...
$.post(url, id, function (data, textStatus) {
...
$("#panel-link").text(data.Link);
}
$("#panel-link").text(data.Link);
obv has to be
$("#panel-link").html(data.Link);
Try using JavaScriptSerializer:
var link = "<b>Hi</b>";
var encoded = new JavaScriptSerializer().Serialize(link);
Try to use javascript's decodeURI() function.
http://www.w3schools.com/jsref/jsref_decodeuri.asp
<script type="text/javascript">
var uri="mytest.asp?name=ståle&car=saab";
document.write(encodeURI(uri)+ "<br />");
document.write(decodeURI(uri));
</script>
The output of the code above will be:
mytest.asp?name=st%C3%A5le&car=saab
mytest.asp?name=ståle&car=saab
I have the same problem with you and killing me whole day,
I solved this problem by using Json.NET
Sample code is :
Newtonsoft.Json.JsonConvert.SerializeObject(link);
Reference
http://json.codeplex.com/documentation

Categories