How do I use a header location in jQuery? [duplicate] - php

This question already has answers here:
How do I redirect to another webpage?
(58 answers)
Closed 9 years ago.
Can I use a header location in jQuery for redirection or refresh?
Like in PHP:
header('location:www.google.co.in');
header("Refresh:1,url=home.php");
If not, what is the alternative way?

Headers are interpreted prior to the rendering of the document, and before jQuery is even loaded so these aren't an option. Instead, you can redirect the browser using document.location.
document.location.href = 'www.google.co.in';
For a jQuery approach you can use
$(location).attr('href', 'www.google.co.in');
however I would favor the plain javascript version.

You can use:
window.location.assign('www.google.co.in');
or
window.location.href = 'www.google.co.in';
or
window.location.replace('www.google.co.in');
The difference is that assign() will just cause a new document to load. While replace() will replace the current document and replace the current history with that URL making it so you can't go back to the previous document loaded.

Use
window.location.href="www.google.co.in";
for redirection.
and
window.location.reload(true);
to reload the page

Related

Is it possible to apply jquery to content rendered by external php (ajax)? [duplicate]

This question already has answers here:
Events triggered by dynamically generated element are not captured by event handler
(5 answers)
Closed 8 years ago.
page-one.php contains such jquery
$.post("___for_ajax_process.php", data_to_send_ajax_post, function(data_edit_content) {
$('#show_result').html(data_edit_content);
});
in #show_result i see content from ___for_ajax_process.php
Rendered content contains many checkboxes and want to create check all. check all is named draft_check_all.
At first want to check what happens if I click the checkbox.
$("#draft_check_all").click(function(){
alert ( 'click test' );
});
And I do not see alert ( 'click test' ); No popup at all.
Then View source and I do not see source code for content rendered by ___for_ajax_process.php
As understand in such way can not apply jquery to content from ___for_ajax_process.php? What if place $("#draft_check_all").click(function(){ in ___for_ajax_process.php? Are there any solutions for such situation?
Solution
placed jquery in external php and all works!
A better solution would be event delegation.
$('#show_result').on('click', '#draft_check_all', function () {
...
});

how to hide URL from downloadlink? [duplicate]

This question already has answers here:
Secure files for download
(2 answers)
Closed 8 years ago.
i was wondering, when someone is downloading a file, lets say a .zip file, is it possible to hide the url where the .zip is stored?
I use a link to the .zip file to download it like this:
<a class="BigButton"href="http://www.webprofis.nl/demo/vd/1/validation- contactform/validation-contactform.zip">DOWNLOAD contactform </a>
When mouseover the link, everybody can see where the file is stored...
You can do this only with javascript.
Here is a little example with jQuery:
Download link
<script>
$('a[data-link]').click(function(e) {
e.preventDefault();
window.open($(this).attr('data-link'));
});
</script>
If you make the href='#' then add an onclick event you could use javascript to redirect the user to the zip file. However, if something views the source of your page it'll still show it there.

How to Add PHP Variables in a Jquery Notification [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
passing variables from php to javascript
I am using this awesome jQuery Stick Notification Plugin which creates notifications upon clicking various buttons.
I used the following code to display the notification box...
$(".notify").click(function() {
$.sticky('The page has loaded!');
});
Can i make it display some PHP variables in place of static text message?
Hope i've made it clear enough.
Without making asynchronous HTTP calls, you will have to insert the PHP variable at server-side:
$(".notify").click(function() {
$.sticky('<?php echo htmlentities($message); ?>');
});
Wrap it with htmlentities() just in case $message contains some chars that make the JavaScript string invalid.
yes you can like
$(".notify").click(function() {
$.sticky('<?php echo "The page has been loaded"; ?>');
});

how can change value of a div [duplicate]

This question already has answers here:
How can I convert language of a div?
(4 answers)
Closed 7 years ago.
"<div id="diva">this is div a</div>"
"<div id="divb"> this is div b</div>"
now how can I change the text of div dynamically by button click. here also mention that all text come from a database. when I click the button then it detects the div id and replace the text with desire text.
All code will be in PHP.
This will work once the page is being loaded:
document.getElementById('diva').innerHTML =
<?php echo functionThatReturnsSomeValue(); ?>;
Dynamically, you would need to do an AJAX call on the PHP file which outputs a value. You'd be better off if you used a JS framework such as jQuery, rather than implementing the AJAX call yourself.
PHP cannot modify the content of the page after it has been served to the browser. However this would be trivial with a JavaScript Library such as jQuery.

php code in jquery [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
How do I use PHP variables as values for the <script> tag when rendering jQuery code in my CakePHP view?
In CakePHP, the value passed as a parameter in the url can be obtained by the controller attribute
<?php $userid= $this->params['pass'][0];?>
I want to use $userid inside the jQuery code.
$("#displayPanel #saveForm").live("click", function(){
document.location = 'http://localhost/cake_1_2/forms/homepage';
});//Click on SaveForm
Suppose if the userid is 12, I need the document.location to be 'http://localhost/cake_1_2/forms/homepage/12'.
How to use the php variable in jQuery?
For readability, how about:
<?php $userid= $this->params['pass'][0];?>
var userId = '<?=$userid?>';
$("#displayPanel #saveForm").live("click", function(){
document.location = 'http://localhost/cake_1_2/forms/homepage/' + userId;
});
You'll need to output the variable into the JavaScript source:
$("#displayPanel #saveForm").live("click", function(){
document.location = 'http://localhost/cake_1_2/forms/homepage/<?=$userid?>';
});//Click on SaveForm
Note the <?=$userid?>. If shorthand is turned off on your server, use <?php echo $userid; ?>.
Apart from the approaches suggested above, you could also:
Use the built in CakePHP Javascript helper to tie the PHP and JS parts better together (better code separation and no code language mix-up). Have a look here: http://book.cakephp.org/view/207/Javascript
Create a hidden input field in the HTML with you passed value that the jQuery can read from. Might seem somewhat over-complex, but can be handy in cases where the jQuery should be able to modify the values also or be used in submit forms etc.
Make the CakePHP controllers responsible for reading and using the user id instead of the JS. Depends on your specific use case, but instead of passing the parameter to the new page, simply fetch the value as the first thing from the controller that you call (impossible if the user id isn't always the same or can't be predicted passively).

Categories