here I want to display the data id based on the click button using js
my code:
var idPost = document.getElementsByClassName("getID")[0];
idPost.onclick = function() {
var GetID = window.location.protocol + "//"
+ window.location.host
+ window.location.pathname
+ '?id=<?php echo $id;?>';
window.history.pushState({ path: GetID }, '', GetID );
}
why my code isn't working?
maybe someone here can help my problem
This may will help you:
EDIT
location.href reloads page to indicated url
var idPost = document.getElementsByClassName("getID")[0];
idPost.onclick = function() {
let id = "<?php echo $id; ?>";
location.href = `?id=${id}`;
}
Related
Face problem when writing PHP syntax in javascript syntax.
var id = $("#data-1").val();
var url = '<?= base_url('home/alone/'); ?>.'id'';
console.log(url);
I need to place the id at the end of url. But my code is not working. What is wrong?
Thank you in advance.
use " instead of ' like this:
var id = $("#data-1").val();
var url = "<?= base_url('home/alone/'); ?>" + id;
console.log(url)
My code below opens a random link and instead of opening the link in a new page I want it to open in an Iframe is this possible.
<button onclick="randomLink()" target="iframe_a";>Click here to go somewhere else!</button>
var randomLink = function () {
var links = [
"bing.com",
"google.com"
];
var max = (links.length)
var randomNumber = Math.floor(Math.random()*max);
var link = links[randomNumber];
window.location = "http://" + link ;
}
</script>
<iframe src="demo_iframe.htm" name="iframe_a"></iframe>
Simply change the src attribute of the iframe, replacing the window.location = "http://" + link ; line to the following:
document.getElementById('iframe_a').setAttribute('src', "https://" + link);
Or if you want to use jQuery:
$('iframe').attr('src', "https://" + link);
Just like in the #DinoMyte comment.
The javascript code would look like this:
var randomLink = function () {
var links = [
"bing.com",
"google.com"
];
var max = (links.length);
var randomNumber = Math.floor(Math.random()*max);
var link = links[randomNumber];
document.getElementById('iframe_a').setAttribute('src', "https://" + link);
};
The complete example is here: https://jsbin.com/cuzehu/1/edit?html,js,output
I want to add an action to the form on change so that when you select an option in the form you go to the url I have set in my JS with that parameter selected in the option.
In the below code I have two alerts, these both get the desired url. And they even take me to a page where the url seems to be ok, but the content doesn't seem to load.
If my url is: http://www.example.com/index.php?a=108&tg=2 in the browser, after I select the form it functions as if I only have http://www.example.com/index.php.
See fiddle
<div id="myID">108</div>
<form class="aclass" target="_blank" method="post">
<select>
<option value="1">aaa</option>
<option value="2">bbb</option>
</select>
</form>
<script>
var pageurl = window.location.protocol + "//" + window.location.host + window.location.pathname;
var myID = jQuery('#myID').html() || '';
var myURL = '?a=' + myID + '&tg=';
jQuery('.aclass select').change(function () {
var opVal = jQuery(this).val() || 0;
var finalURL = pageurl + myURL + opVal;
alert(finalURL);
jQuery(this).closest(".aclass").attr("action", finalURL);
alert(jQuery(this).closest(".aclass").attr('action'));
this.form.submit();
});
</script>
What am I doing wrong here how can I actually get to have http://www.example.com/index.php?a=108&tg=2 from my form?
If you need to submit the form using POST, then the actual querystring as they're processed as GET variables. You can create hidden input fields and add them to the form prior to submission to receive those variables as POST variables.
var pageurl = <?php echo $_SERVER['PHP_SELF']; ?>;
var myID = jQuery('#myID').html() || '';
var myURL = '?a=' + myID + '&tg=';
jQuery('.aclass select').change(function () {
var opVal = jQuery(this).val() || 0;
alert(opVal);
var finalURL = pageurl + myURL + opVal;
alert(finalURL);
jQuery(this).closest(".aclass").attr("action", finalURL);
alert(jQuery(this).closest(".aclass").attr('action'));
this.form.submit();
});
Please try this
My Url looks like: http://google.com/stackoverflow/post/1
Now, I want to get just part of URL: http://google.com/stackoverflow/ and add it to code:
Post . How to do it ? Thanks !
Plz try this:
var url = "http://google.com/stackoverflow/post/1";
var partOfUrl = url.split('post')[0];
Thanks.
UPDATED:
Using JavaScript:
var url = "http://google.com/stackoverflow/post/1"; // or var url = window.location;
var matches = url.match(/^http\:\/\/([\w\.]+)\/(\w+)\/.+/);
if( matches ){
var newURL = "http://" + matches[1] + "/" + matches[2] + "/";
alert( newURL );
}
document.getElementById('post_link').href = newURL;
HTML:
<a id="post_link">post</a>
See JSFiddle
use location Object
location.href
location.pathname
I'm getting confused passing the http:// in a string to the url as it is being stripped to
http%3A%2F%2F
I tried.. using the encodeURIComponent(http://)
but that didn't work either..
I'm trying to pass the url into here:
https://www.facebook.com/sharer/sharer.php?url=
Here is my code that isn't working:
$(document).on('click', '.fb', function(e) {
var findfb = $('.fb').parent().parent().parent().find(".zoomy").attr("src");
var facebook_url = 'http://www.facebook.com/sharer.php?url=http://www.site.com/folder1/'+encodeURIComponent(findfb)+
'&title='+encodeURIComponent('title of page');
window.open(facebook_url);
});
Just do it as simple as:
var facebook_url = 'http://www.facebook.com/sharer.php?url=' + encodeURIComponent('http://www.site.com/folder1/' + findfb +'&title=' + 'title of page');
var facebook_url = 'http://www.facebook.com/sharer.php?url=' + encodeURIComponent('http://www.site.com/folder1/' + findfb) + '&title=' + encodeURIComponent('title of page');