How to make a window pop up from a link using javascript - php

I am making a bookmarklet for my url shortener. What it does is it shortens the current url of a page when you click on the bookmarklet, so you don't have to go to a site to shorten a url, but what I need is for the bookmarklet to return a pop-up window with the shortened url listed below.
What I've done so far is this:
javascript:u=encodeURIComponent(location.href);s='http://n1x.co.uk/create.php?url='+u;window.open(s,'shortened','location=no,width=400,height=300');
If you have bookmarked this, you will notice when you click on it, it does shorten the url of the current page, but it also shows the url shortener webpage, and what I only need is plain text and a link to copy it.
Please help!
Edit: Sorry I'm that bad at php, but how can I make a parameter "responseType"? Thanks

What is shown in the popup is what is returned by http://n1x.co.uk/create.php. You will have to modify the php to return only the shortened url in plain text. If the php is being used by the shortener site also, what you can do is pass a parameter to create.php that tells it whether to return the whole page like it does now or to return only the plain text url. For example a parameter called 'responseType'. You can modify the create.php to say that when responseType = 'plainurl', return simply the shortened url and not the whole page.
javascript:u=encodeURIComponent(location.href);s='http://n1x.co.uk/create.php?url='+u + '&responseType=plainurl';window.open(s,'shortened','location=no,width=400,height=300');

Related

url change without anchor tag and refresh using ajax

As per my client need , redirect the page without anchor tag and refresh page ,
and change the URL as per page appearance. I don't know any idea about this.
I can use ajax for page content. but I don't know the URL change option without
page refresh or redirect. how can I do this with ajax j-query. Any one guide me for this issue.thanks advance
sample url
www.samplesite.com/contact.php -> without anchor tag. and page refresh this url need to work on php.
I think you are looking for info about the new HTML5 History API ( pushstate ), this link has a detailed tutorial.
http://diveintohtml5.info/history.html
You can do this using below function.
if(pageurl!=window.location){
window.history.pushState({path:pageurl},'',pageurl);
}
You can use the following javascript functions
window.location.assign('http://www.samplesite.com/contact.php'); // keeps the current page in browser history
window.location.replace('http://www.samplesite.com/contact.php'); // replaces the current page in browser history
window.location = 'http://www.samplesite.com/contact.php'; // the same as assign() and is backward compatible even with the oldest browsers
jsfiddle

How to put php GET params in a HTML file?

I have a PHP file which returns me a JSON array based on the GET parameters set. I don't want to reload the page so I made a simple function using jQuery + AJAX to show the result in a div tag.
But there is a problem, because of using AJAX doesn't change the URL and the user won't be able to share the link.
The HTML is called 'calculate.html' and the GET URL would be something like this:
myweb.com/calculate.php?sex&female&age=23&brand=pull%26bear&msg=hello%20stackoverflow
I'd like to know what is the best practice to modify the URL of the HTML when the AJAX call returns the result. And when someone open this URL the HTML reads it and then executes the call with these parameters.
I've modify the .htaccess and this is the url of the html:
myweb.com/calculate/
And with the parameters:
myweb.com/calculate/female/23/pull%26bear/hello%20stackoverflow
Any tip, advice or help would be appreciated, and if you need more information, let me knoe and I'll edit the post.
You can use a # tag in your url
eg http://yoursite.com?param1=23#tempcode
So you can call the ajax code when the url has the #tag tempcode.
In this way you can share your new url
On complete of your AJAX request, you can redirect the page url using
window.location.replace("myweb.com/calculate/female/23/pull%26bear/hello%20stackoverflow");

Codeigniter url gets appended every click

I have a problem with CI whenever i click a button in a form which has an action of image/upload or a hyperlink with the same link it gets appended whenever i click it the second time. say for example my home is localhost/admin and i click a button or a link which has image/upload.. so the url will now beh localhost/admin/image/upload but when i click the same button the second time the url will now beh calhost/admin/image/image/upload wchich well then cause a 404 error which ofcourse is the error given that the page is not found by just seeing that url. it gets appended every time i click the button or the link.
Anyone of you knows this please do share!
UPDATES:
BTW just a headsup for all those people who didn't know or who encountered this problem.. USE anchor or any helper in CI becuase if you manually put links in href or actions on form tag without putting the base_url.. your URL will be messed up.. helpers do append base_url. :D
Check out the Codeigniter docs and the URL Helper. That should help out.
http://ellislab.com/codeigniter/user-guide/helpers/url_helper.html
I know for anchors you would just do:
echo anchor('image/upload', 'Upload');
This will append the url to the base url and you don't have to worry about changing anything or any 404 errors.
just put http:// at infront of your link.
or otherwise
change your config file:
$config['base_url'] = 'http://www.yourhost.com/home';
try to link the url as '/path-to-url'. notice '/' before url.
Link
currently you might be doing like below
Link
Also try changing
$config['base_url'] = 'http://localhost/home/';

Masking urls in javascript

I have a url1.
But I have seen on websites that they would use javascript to show this as url1 but it is actually url1 which redirects to url2.
You would not see the original url when you hover your mouse over the link.
The same thing is also used on google's search results. Can you please suggest how it is done. :)
You can use JavaScript onclick event to change the href on click.
Example: link
In this example users see that the link url is url1 but when they click on it goes to url2.
For a simple, example, you could make a redirect script that just takes the url out of a GET parameter, and then redirects the user to that page.
redirect.php:
<?php
$Link = $_GET['l'];
// Do whatever you want with $Link here (store it in a database, for example)
header('Location: ' . $Link);
?>
Now, instead of linking to url1, you can link to redirect.php?l=url1.
Please keep in mind that in an actual environment, you'd probably want to do some sanity checks on $Link before redirecting the user there.

Variable from JavaScript to PHP

Here is the code:
$('#sousmenu a').click (function (){
startSlideshow(<?php echo json_encode(glob("photos-" .$_GET["folder"]. "/*.jpg"));?>);
return false;
});
The question is I like the HREF to change and get caught by PHP, now it doesn't do anything, but writing the ?folder=portraits works.
Here is the page.
**** Simpler *****
Maybe I am not clear, it happens sometimes!
I want the link href to be send to this PHP function,
<?php echo json_encode(glob("photos-" .(i what the href link). "/*.jpg"));?>
so clicking on the link animaux will send animaux to the glob() PHP function and will get all the .jpg files in the photos-animaux folder.
Clicking on the portraits will send the photo-portraits, etc.
If you want to modify the URL and have the added/changed variable picked by PHP interpreter you have to reload your page. Just altering the URL doesn't do anything because JS is executed after PHP processing.
If your site is on http://example.com and you wish a myparam with value test to be passed to PHP you should add something like this in your JS:
document.location = 'http://example.com?myparam=test';
This will reload your page adding a new param which can be accessed in PHP by simply using $_GET['myparam'] variable.
You may also want to consider using AJAX to dynamically changing the contents of your page without having to refresh the whole page, but that's a little bit more complicated.
Look at the source in your browser.
Php is server-side, and that means you need to use ajax or reload whole page to get a response.
There is a nice ajax part of tutorial on jquery website, after reading it you should be able to do what you want: http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery#Rate_me:_Using_Ajax

Categories