Is it possible to run the Cufon text replacement script in PHP (or before its sent to the browser)? The reason I ask is that there is a bit of an issue with the displayed HTML being its normal browser rendered text before Cufon is able to draw its magic over it. The user sees a flash of unrendered text (FOUT) before it is replaced with Cufon's awesomeness. I've noticed that the rendered HTML has some tags generated in place of the HTML (canvas and Cufon tags) text and I thought, what if this could be done in PHP and then sent to the browser so that the browser actually receives the drawn text from the start?. Would this mean porting over the code that draws the text to PHP? This came as a stroke of genius or more likely stupidity last night and was wondering if anyone had some thoughts on the matter. Thanks for reading.
Cufon.replace('div#nav-menu a h5',{
fontFamily:'United Stencil',
hover: true,
hoverables : {h5 : true}
});
Cufon.replace('.stencil',{fontFamily:'United Stencil'})
Cufon.replace('.heavy',{
fontFamily : 'United Heavy',
hover : true,
hoverables : {
h1:true,
h2:true,
h3:true
}
});
Here is the Cufoned HTML:
<a class=" heavy" href="/mp_svn/node/5">
<cufon class="cufon cufon-canvas" alt="Products" style="width: 65px; height: 16px;">
<canvas width="77" height="17" style="width: 77px; height: 17px; top: -2px; left: -2px;"></canvas>
<cufontext>Products</cufontext>
</cufon>
I would like to send the above HTML to the browser from the beginning, pre-Cufon it is something like:
Products
Cufon has a callback function that runs after all text has been replaced: http://groups.google.com/group/cufon/browse_thread/thread/20a8d2edd45f1aa5/1a498d21856405cd
You could either
Initially hide the content with CSS, then show it with javascript in the callback, and be absolutely sure you will have no FOUT--but your page will be totally inaccessible for users without JS
Or, hide the content with JS when the DOM is ready, and then show it again after the text replacement has occurred. Using jQuery,
$(document).ready(function(){
$('#content').css('visibility', 'hidden');
Cufon.CSS.ready(function() {
$('#content').css('visibility, 'visible');
});
});
This should work fine for Firefox/webkit, but there will still be a FOUC in IE before the JS takes effect. You could also use $('#content').fadeTo(0, 0); if you want to be able to fade in the content once it's been replaced by Cufon.
EDIT
I have figured out a superior way. You go with the #1 approach, hiding content with CSS, but then you throw in a <noscript> area with a style declaration that resets visibility:visible
That way there will be absolutely no FOUC, and if JS is enabled, they won't have a problem either.
You could use Cufon's built in function
Cufon.now();
So there's no flash when replacing the text.
Great idea, and it's something people have been doing for a while. Here's an early one from 2004: http://www.alistapart.com/articles/dynatext/
And a few newer ones
http://artypapers.com/csshelppile/pcdtr/
http://www.joaomak.net/util/dtr/
I set my visibility to false in css, call the replace function on Cufon and use the onAfterReplace callback to set the element to visible.
$(document).ready(function(){
Cufon.replace('h1', { fontFamily: 'alwyn-thin',
onAfterReplace:function(el,options){
$(el).css('visibility', 'visible');
}});
Related
In my gallery I want to show my images, but I have two formats of images: 1/1:square format, 2/3:portrait format.
I created a class (result) for my ul container and I add the images to it. By modifying some properties I center it vertically and horizontally. Some images appear smaller because of their format I can't just let width (8em).
My question is: Can I add frm as an attribute to my image tag like src? If yes , I'll have this value from my database.
My script doesn't work. My Javascript is:
$(document).ready(function() {
if ($("ul.result li img").attr('frm')='1/1')
{
$("ul.result li img").css('width','10em');
}
if ($("ul.result li img").attr('frm')='2/3')
{
$("ul.result li img").css('width','8em');
}
});
Since you are using jQuery you can use the data method (http://api.jquery.com/data/) and properties on the elements in your html to store whatever it is you want. For your reference, that looks like:
HTML
<img src="file" data-format="square" />
Javascript
$(function () {
$("img").each(function () {
if ($(this).data("format") == "square") {
// process square image element ("this")
} else {
// process normal image
}
});
});
Now, that said - as others have noted you might consider using css classes to change your style rather than writing a bunch of Javascript code. The only reason I would consider writing the javascript (as opposed to css) is if there are complicated calculations that need to happen. Most of the time, though, if you structure your html correctly you can do whatever you need with pure css (no Javascript).
Why are you even using javascript for this?
You have a database property, and you're changing a css property based on a database property. That's what classes are for!
<img class=<?php (img.frm=="1/1"?"square":"portrait") ?> src=..... />
Then, in your css:
.square{
width: 10em;
}
.portrait{
width: 8em;
}
If you want to add a home made attribute, use a HTML5 data attribute,
so it is valid : data-portrait and data-landscape will do fine.
those attributes can be access via CSS or js
To answer your question:
YES you can, but i advise to use :data-frm as attribute name and avoid the use of special caracter in values.
Some reading from W3C :)
I have this html code where links' target is an iframe.
So I want to add some style, highlighted links. I want to make the active link highlighted only with CSS( or/with php), I'm trying to avoid javascript.
Here is the code:
<ul>
<li>Rhenium</li>
<li>Tellurium</li>
<li>Tin</li>
</ul>
<iframe name="box" src="http://en.wikipedia.org/wiki/Rhenium"></iframe>
Best view: http://jsfiddle.net/WB8e8/
I've tried a lot of css tricks, including a non displayed iframe and combined it with different selectors (~ + >) with < li >, but it only goes messy and make css size big.
I've tried some php conditions too, I've couldn't manage to accomplish a result.
So, how to make this work? how to make a highlighted link when iframe displays content of the url?
Why not some JavaScript?
$(function() {
$('ul li').click( function() {
$(this).addClass('active').siblings().removeClass('active');
});
});
DEMO
You can use the CSS3 attribute selector:
a[target="box"]:active {
font-weight:bold;
}
I'm going mad. I get the value (it's the address of an home) from a Wordpress custom field (input type) with this code:
<?php $dudi=get_post_meta($post->ID, 'addr', true); ?>
It's good! Now I have to "pass" this variable $dudi to a jQuery function (it's gMap, for display Google maps):
$(function() {
$("#map").gMap({ markers: [
{ address: '<?php echo $dudi; ?>',
html: "blah blah<br>blah blah" }
],
zoom: 10 });
});
and now with the tag <div id="map"></div> gMap should display the map with the marker on the specified address (value in the variable $dudi) but doesn't work!
Notice that if I manually write the address (in place of <?php echo $dudi; ?>) it works on Chrome with bugs (strange effects when click the marker and move the map) and don't works on IE and Firefox
Don't know... I've tried in any way but nothing to do... and I need this for tomorrow! :(
Hope in you guys!!
UPDATE:
Oh my God! I'm going for steps because it's not clear if the issue is caused by the passing of the variable or by a html/css conflict. So for the moment I have manually written the address in the jQuery function bypassing the variable.
Well, after many headaches I've found that the map goes in conflict with the css:
max-width: 100%;
Infact, when I comment that line the map shows correctly.
NOW, I restored the variable and now the map doesn't work on any browser (aka gray backgroud) BUT it shows correctly the zoom fader and in the browser console I have two errors TypeError: a is null in the file main.js. Unfortunately I don't know this file and I'm not able to fix some codes.
Hope you guys... again!
if needed...
1st error
H(ff,hb);var Kh=256/360,Lh=256/(2*Math.PI);ff.prototype.fromLatLngToPixel=function(a,b){var c=128+a.lng()*Kh,d=Db(Math.sin(Xb(a.lat())),-0.9999,0.9999),d=128+0.5*Math.log((1+d)/(1-d))*-Lh,f=1<<b;return new r(u(c*f),u(d*f))};
2nd error
Oh(Sj,"arrow",1);function qi(a,b,c){!a.lat&&!a.lon&&(a=new P(a.y,a.x));this.Aa=a;this.Nu=i;this.na=0;this.N=this.mb=!1;this.Oo=[];this.V=[];this.Ra=Mj;this.Mg=this.cl=i;this.Ub=!0;this.Dg=this.rf=!1;this.g=i;if(b instanceof Qj||b==i||c!=i)this.Ra=b||Mj,this.Ub=!c,this.ha={icon:this.Ra,clickable:this.Ub};else{b=this.ha=b||{};this.Ra=b.icon||Mj;this.mv&&this.mv(b);if(b.clickable!=i)this.Ub=b.clickable;if(b.isPng)this.rf=!0}b&&Ob(this,b,"id,icon_id,name,description,snippet,nodeData".split(","));this.Zu=Tj;if(b&&b.getDomId)this.Zu=
"Chrome shows the map with the marker OK!!! While Firefox and IE don't
show nothing (gray background, etc)"
If Ennui's solution works fine at Chrome, then check the other browsers consoles - they should output some kind of errors. That's what usually happens when GoogleMaps show 'gray background' instead of a map.
A quick and dirty solution would be to echo the $dudi variable somewhere on the page in a hidden div. For example:
<p style="display:none" id="dudi">
<?php echo get_post_meta($post->ID, 'addr', true); ?>
</p>
Then in your jQuery:
var dudi = $('p#dudi').text();
Didn't have time to test this but I think it should work for your purposes.
try to find css stype for img tag
max-width: none;
and replace it to:
max-width: none;
I have a small problem with my PHP code and It would be very nice if someone could help me. I want to display an image when hovering over a link. This is the link with the PHP code that I have now:
<?php if ( has_post_thumbnail() ) {the_post_thumbnail();} else if ( has_post_video() ) {the_post_video_image();}?>
This code shows a image, but I want to execute this code when hovering over the link with the image:
<?php echo print_image_function(); ?>
The code also shows a image that belongs to a category. I don't want the initial image to disappear I simply want to show the second image on top off the first image when hovering over the first image.
I don't know if it is helpful but I use Wordpress and I am not a PHP expert. I even don't know if this is going to work. Thats why I am asking if somebody can help me with this.
Thanks in advance
THANKS EVERYONE
I want to thank everybody that took the time to read my post and helped me by giving their solution.
I didnt exspect so many answers in such a fast time. After spending a few hours trying it to get it to work with PHP, CSS and Javacript, I stumbled upon the following question on this website: Solution
It was exactly where I was looking for and with a few modifications to fit my needs, I got it to work. Sometimes things can be so easy while you are looking for the hard way. So for everyone that has the same problem: You can use one of the solutions that where given by the awesome people here or take a look at the link above.
Thanks again! :)
You can do this with CSS (if you so please and this fits with your overall architecture) - here is an example using the :hover condition and :after pseudo element.
html
<img src="http://www.gravatar.com/avatar/e5b801f3e9b405c4feb5a4461aff73c2?s=32&d=identicon&r=PG" />
css
.foo {
position: relative;
}
.foo:hover:after {
content: ' ';
background-image: url(http://www.gravatar.com/avatar/ca536e1d909e8d58cba0fdb55be0c6c5?s=32&d=identicon&r=PG);
position: absolute;
top: 10px;
left: 10px;
height: 32px;
width: 32px;
}
http://jsfiddle.net/rlemon/3kWhf/ demo here
Edit:
Always when using new or experimental CSS features reference a compatibility chart http://caniuse.com/ to ensure you are still in your supported browsers. For example, :after is only supported starting IE8.
You cannot randomly execute server side code on the client side.
Try using javascript AJAX requests instead.
PHP is a server-side language; you can't get PHP to execute after the page has loaded (because PHP completely finishes parsing before the page loads). If you want hover events, you need JS.
Firstly you don't need the elseif statement. An else will serve the same purpose, unless you intend to have blank tags where neither a thumbnail or a video image are present.
<a href="<?php the_permalink(); ?>">
<?php
if ( has_post_thumbnail() )
{
the_post_thumbnail();
}
else
{
the_post_video_image();
}
?>
</a>
You can't explicitly use PHP for client side functionality. You will need to use javascript or jquery to supply the on hover capability.
Jquery example:
$(function() {
$('a').hover(function() {
// Code to execute whenever the <a> is hovered over
// Example: Whenever THIS <a> tag is hovered over, display
// the hidden image that has a class of .rollover
$(this + ' .rollover').fadeIn(300);
}, function() {
// Execute this code when the mouse exits the hover area
// Example (inline with above example)
$(this + ' .rollover').fadeOut(300);
});
});
To have an image placed on top of another image you would need to make sure your CSS uses absolute positioning for the images with the image that is to overlay the other on hover is given a z-index value higher than the image to sit underneath it.
Hope this helps.
You'll need some JavaScript and/or CSS to make this work, since PHP is on the server side, not in the client browser.
I am building a website that displays recipes. Each recipe appears as part of a blog entry, and will have a link at the bottom to Print this recipe.
What I want to happen is a click on the link opens a new window and fills it with a print-friendly-styled version of the recipe, which is already inside its own <div class="recipe">.
Can I do this with JS/jQuery alone, or do I need to process from the server side? Any ideas how to do this?
EDIT: What would be ideal would be to generate a PDF on the fly, but in lieu of that I'd like a new window, containing only the recipe, for the visitor to print out or save, as they see fit. Print-styles are nice, but most people don't know they exist and can't be bothered to check and see by printing out a page that doesn't look print ready.
There is no need to load a different stylesheet and the only javascript you will need is for triggering the printing dialog.
With CSS alone you can add rules that are only used when printing, you can either use media queries
<style type="text/css">
#media print{
//css printing rules
}
</style>
or use the link tag:
<link rel="stylesheet" media="print" href="styles.css" type="text/css" />
UPDATE: If you want to update the stylesheet on the fly without openning a new window i suggest you check out this Nettuts article or a simpler solution:
$("#css-switch").click(function() {
$("link[rel=stylesheet]").attr({href : "red.css"});
});
You can do this simply with CSS alone if you have it load the exact same page but with a different stylesheet.
Yes, you can(Hello Mr. Obama).
Most browsers allow you to pass in a data: format string, like
window.open('data:text/html;charset=utf-8,text%20to%20show');
which would open a new window / tab (that is browser config dependend) with the Content "text to show". You can pass in HTML code in the same manner, probably escaped.
var print = $('<div>', {
id: 'foobar',
html: 'Hello world',
css: {
backgroundColor: '#ff0000',
color: '#ffffff',
width: '200px',
height: '200px'
}
}),
opener = $('<div>').append(print);
window.open('data:text/html;charset=utf-8,' + opener.html());
Demo: http://www.jsfiddle.net/4yUqL/73/
You'll probably need to do a request to the server on the print page and fill in the form fields with the data.
If i was you, i'd go for the pdf creating solution. It is fairly simple to create pdf's on the fly in php, and it will almost certainly give a better user experience to your main audience.
If you want to skip that excersise, i'd do it the following way:
1: fetch the data you need as JSON on the original non-printerfriendly page, and use clientside templating to build the ui. Store the JSON for use on the printerfriendly page
2: when you open the new window, use the exact same method, but use another template optimized for printing.