To clarify:
I have an echo statement as follows:
$striptitle .= ' - <a onclick="getnewurl();" href="'.SGLink::album($aid). '">'. $namek .'</a></h1>
<a href="https://twitter.com/share" class="twitter-share-button" data-lang="en" data-via="jb_thehot" data-text="Pictures of '. $hashfinal .'" teens>Tweet</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>';
echo $striptitle ;
The onclick does this:
<script type="text/javascript">
function getnewurl()
{
var url = document.URL;
alert( url );
}
</script>
What I need is essentially to add the my anchor tag a data-url="INSERT_VAR_URL_HERE"
Is that possible?
Let me know if this isn't clear enough
Thanks!
Edit: to clarify, the alert in the function is only for testing. What I need is to be able to use the variable obtained in the function, in the same $striptitle variable.
My problem is that the url changes with AJAX, and the twitter button's data-url does not get updated. I was hoping to be able to get the new url by getting it everytime it is clicked. If there are other ways to do that, I'm open to suggestions!
Why not pass it as an argument?
$striptitle .= " - <a onclick=\"getnewurl('URL-HERE');\"...>";
Then in your script:
<script>
function getnewurl(url) {
alert(url);
}
</script>
Building on #Kolink's answer one way to not repeat the url in two places you could generate the link as:
<a onclick="getnewurl(this);" href="...">
then your JS could look like:
function getnewurl(link) {
link.setAttribute('data-url', location.href);
}
EDIT apparently jQuery.data doesn't update the dom properly, but setAttribute does
Related
This might be a popular question but I couldn't find the answer I'm looking for.
Anyway What I want to do is pass an option value in the parent window to a popup window so I can display some thing in the popup according to the selected option.here is my code of the parent window.
<select name="itemSelect" id="itemSelect">
<option>chair</option>
<option>table</option>
</select>
<a class="button" href="#" onclick= "MyWindow=window.open('form3.php','MyWindow','toolbar=no,location=no,directories=no,status=no, menubar=no,scrollbars=no,resizable=no,width=500,height=650'); return false;">enter data</a>
Using jQuery, you could do something this.
value = $('#itemSelect option:selected').val();
window.open('form3.php?value='+value+', MyWindow
You could then look for $_GET['value'] on the PHP page and process it!
<a class="button" href="#" onclick= "MyWindow=window.open('form3.php?itemSelect='+document.getElementById('itemSelect').value,'MyWindow','toolbar=no,location=no,directories=no,status=no, menubar=no,scrollbars=no,resizable=no,width=500,height=650'); return false;">enter data</a>
Then in form3.php
echo $itemSelect = $_GET['itemSelect'];
You can do this via special onclick handler by adding your value via hash or url param:
<a class="button" href="#" onclick= "openNewWindow()">enter data</a>
JS:
function openNewWindow() {
var your_select = document.getElementById("itemSelect");
var value = your_select.options[your_select.selectedIndex].value;
MyWindow=window.open('form3.php#' + value,
'MyWindow',
'toolbar=no,location=no,...,width=500,height=650');
// ... or
// MyWindow = window.open('form3.php?select_value=' + value, ...
return false;
}
You want to pass the selected option to a pop up window for better interaction?
It would be easier to use alert() in js; using onclick handler you can call a function in js which then displays the pop up by using alert(document.getElementById().value=variable)
I have been looking online, but I have failed to find a direct answer to my question:
Can you use onClick for a tags?
In other words can you do this (note echoed through php)?
echo "\n";
I tried to run this, but when I click on the link, my counter function is not being called.
The rest of my code snippet:
echo "<script type=\"text/javascript\" src=\"src\jquery\jquery.js\">\n";
echo "function counter(){\n";
echo "alert(\"HELLO WORLD!\");\n";
echo "}\n";
echo "</script>\n";
echo "\n";
Although that should work, it's better practice not to bind events inline. I would suggest looking into addEventListener and for older versions of IE, attachEvent. More information on these can be found in a topic here: Correct usage of addEventListener() / attachEvent()?
If you wait for the window to be ready, you ensure that the element is on the page and defined for you to access it.
window.onload = function(){
//add any event listeners using the above methods in here
}
Example:
<script>
window.onload = function(){
var t = document.getElementById("test");
t.addEventListener("click", sayHi, false);
function sayHi(){
alert("Hi");
}
}
</script>
<div id="test">test</div>
According to your above echo statements, if you are determined to make it work that way then you can try this:
echo "<script type='text/javascript' src='src/jquery/jquery.js'></script>\n";
echo "<script>\n"
echo "function counter(){\n";
echo "alert('HELLO WORLD!');\n";
echo "}\n";
echo "</script>\n";
echo "<a href='#' id ='loginbutton' onClick='counter()'></a>\n";
notice that I closed the script tag including jQuery and added a new opening tag right below it.
EDIT:
Script tags that reference external resources (via the src attribute)
are no longer able to execute script embedded within the tag itself.
Read more here
Made a little Demo code, Note that the function fires and then the href is followed. You can turn this off with JS by returning false.
<script type="text/javascript">
function counter() {
alert("do something here")
var FollowHref_Or_Not = false;
// true would simply follow the href after the function is activated
return FollowHref_Or_Not;
}
</script>
Test Link
To add more context to the comments above, here's a working example. Where I put the HTML comment about simulating an echo, just put your PHP echo line. Also note that I added a return false; This prevents the default link click behavior from executing. Since your href is "#" it would modify your URL to put "#" in the URL so if you used your browser back button you'd still be "stuck" on the same page.
http://jsfiddle.net/6pEbJ/
<html>
<head>
<script type="text/javascript">
function connect()
{
alert('connected!');
}
</script>
</head>
<body>
<!-- simulated echo result here -->
Testing
</body>
</html>
Got two questions:
How can i intgrate a php script to a html document that:
should do his work at the documents startup and return a integer, which can be handled by javascript?
should do his work after a link is clicked and uses the links id?
lets create a example project:
1. php-name: start.php
2. php-name: click.php
html-code:
<!doctype html>
<html>
<head>
<title>test</title>
<script type="text/javascript">
$(document).ready(function(){
});
</head>
<body>
<p><a class="links" id="l1" href="http://www.google.com/">Boerse</a></p>
<p><a class="links" id="l2" href="http://www.google.ch/">My Gully</a></p>
</body>
</html>
1. With javascript you could use jquery to perform a POST HTTP Request that simply posts something like 'BeenSent' with data = 1. Then php checks to see if that isset. If it is then send back the variable to jquery as the function at the end of the ajax() method.
Here's an example
$.ajax({
url: "getInt.php",
type: "POST",
data: {BeenSent: 1},
cache: false,
successCondition: function(result) {
alert("getInt.php echo'd this out on it's file when I sent the request. The content it echo'd back was: " + result);
}
});
2.
For the links you could simply perform a GET HTTP Request, or you could simply define the element's id within the link... like so...
<p><a class="links" id="l1" href="http://www.example.com/?elementid=l1">Hey! I'm a link!</a></p>
Hope this helped! :)
For your first question: An ajax call is probably the nicest way to do it, but you could also do a kind of ugly solution. Have the php-function echo out the value to a hidden element, then fetch the value inside that element from your document.ready function in javascript.
function GetInteger() {
return 10;
}
echo '<span style="display:none" id="test">' . GetInteger() . '</span>';
$(document).ready(function(){
var theValue = $("#test").html();
}
For the second question I would create onclick events that made ajax calls with the wanted value as a parameter, alternatively add the id as a get parameter to the querystring.
i am having problem updating the database.
Basically what i am trying to do is everytime a download link is clicked the download count in the database goes up.
Can someone point me in the right direction as i have been trying to get this right for hours :(
Here is the html.
<div class="button_border_dark"> <a id="linkcounter" href="http://www.derby-web-design-agency.co.uk/freeby-download/<?php echo $freebies_download ; ?>" target="_blank" title="Download">Click To Download File</a></div>
Here is the jquery
<script>
$('#linkcounter').bind('click',function(){
$.post("downloadcount.php",{ linkid: <?php echo $id ; ?>});
});
</script>
Here is the downloadcount.php which i am trying to post data too, so it updates the content.
<?php
require_once("applications/constants/connection.php");
require_once("applications/controllers/basic.php");
if(isset($_REQUEST["linkid"])){
$linkid = sanitise($_POST["linkid"]);
$updatedownload = mysql_query("UPDATE freebies SET download_count=`download_count` +1 WHERE id ='".$linkid."'") OR die(mysql_error());
}
You don't say what is the problem!
Is not incrementing? is incrementing too much? is one process blocking the other? the problem is that people can cheat and make so a file has ben downloaded a million times?
Anyway, I think you code can be simpler.
$('#linkcounter').click(function(){
$("#invisibleiframe").attr("src",$(this).attr("src");
$.post("downloadcount.php",{ linkid: <?php echo $id ; ?>});
return false;
});
this need to create a invisible iframe, that will be the one downloaded the file. after starting this download, the ajax request is made. a single event do the two things. made this way the stuff still works if js is disabled.
I think should be
$updatedownload = mysql_query("UPDATE freebies SET download_count= download_count +1 WHERE id ='".$linkid."'") OR die(mysql_error());
Note the single quote:
SET download_count = download_count +1
Well first off I would not mix the jQuery and PHP personally, this may be the source of your problem, I would try something like this
<div class="button_border_dark" theLinkId="<?php echo $id ; ?>">
<a id="linkcounter" href="http://yourURL/" target="_blank" title="Download">
Click To Download File
</a>
</div>
With the jQuery like this
<script>
$('#linkcounter').bind('click',function(){
var theLinkId = $(this).parent().attr('theLinkId');
$.post("downloadcount.php",{ linkid : theLinkId});
});
</script>
It is possible that your event is not actually being bound to the anchor, because you are running .bind() before the anchor exists. Try this:
<script>
$(document).ready(function(){
$('#linkcounter').bind('click',function(){
$.post("downloadcount.php",{ linkid: <?php echo $id ; ?>});
});
})
</script>
how many anchors you have with id = 'linkcounter' ?
If you have more than one, i recommend you to change 'id' for 'class'
Greatings.
EDIT:
Try something like this:
Link
<script type="text/javascript">
function goToLink(o, id) {
$.post("downloadcounter.php",
{linkid : id},
function () {
window.open($(this).attr("href"));
}
);
return false;
}
</script>
Hello to all jQuery enthusiasts and experts!!
I currently use on a project this jquery slider http://spaceforaname.com/galleryview/.
The slider itself doesnt support on mouseover pause and on href click redirect.
The first part was easy to implement
dom.gv_panelWrap.bind('mouseover.galleryview',function(){
self.stopSlideshow();
}).bind('mouseout.galleryview',function(){
self.startSlideshow();
});
As for the links part i tried to do something like this
dom.gv_panelWrap.bind('click.galleryview',function(){
var href="page.php?lang=<#language#>&action=show&objectid=<#objectid#>";
window.location.href = href;
});
As you may understand this doesnt work since the link has php parametrs in it.
Of course when i change the href variable to page.php?lang=en&action=show&objectid=1 i get the page with the infromation about object n.1.
Any ideas of how i could pass the actual href through jQuery?
Thanks in advance for reading this!!!
dom.gv_panelWrap.bind('click.galleryview',
function(){
var href = 'page.php?lang=' + <?php echo "'" . $language . "'"; ?> + '&action=show&objectid=' + <?php echo "'" . $objectid . "'"; ?>;
window.location.href = href;
});
Change the language and objectid variables if needed
I'm not sure of what is what you truly want.
I don't know where are you going to get the id.
From the image thats being shown?
This is the src of the img that's been displaying.
$(".gv_panel img").attr("src")
I guess you can compare with your list of objects which is this image from.