add class to php generated element with jquery - php

I need some help. I am bringing in a string from a custom field in wordpress. This string is an iframe embed code for both youtube and vimeo. I need to add a class to this iframe in order for fancybox to do its thing.
<?php
$videoLinks=get_post_meta($post->ID, "iframe video embed code", true);
echo $videoLinks;?>
<script>
$('iframe').addClass('fancybox fancybox.iframe');
</script>
When testing by inserting an iframe embed code in the custom field I get this. (no class added)
<iframe src="http://player.vimeo.com/video/33039612?title=0&byline=0&portrait=0" width="400" height="225" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe><p>HG Skis: Trillinton.. from HG Skis on Vimeo.</p> <script>
$('iframe').addClass('fancybox fancybox.iframe');
</script>
Relatively new to jquery, fancybox works on images manually tagged with fancybox class. Will adding class based on a certain event fix my issue?

try
<script>
$(document).ready(function(){
$('iframe').addClass('fancybox fancybox.iframe');
});
</script>
it should play the trick... of course, I assume you are using fancybox v2.x

It's possibly about timing. If fancybox is operating via a script that fires automatically on DOM ready, looking for "fancybox" elements to attach event handlers to, it might have already done its job by the time your addClass script fires. Something tells me this isn't necessarily the problem, though; the document ready function will actually be LATER than scripts running before the page is fully built.
There's also a syntax issue: 'fancybox fancybox.iframe' is not valid. You can add space separated classes if you want to add multiple, but "fancybox.iframe" is not a valid class. I believe JavaScript will let you add it, but you can't style it and there might be problems selecting it.
Don't you simply need $('iframe').addClass('fancybox')?
I'd make sure that your addClass is succeeding first, and if it is, are the classes required all present and accounted for. Then, debug timing issues.

The code you have shown works fine. That is, when I open a file with this it works:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<iframe>sfdfsf</iframe>
<script>
$('iframe').addClass('fancybox fancybox.iframe');
</script>
What does your fancybox code look like?
You can turn youtube links into embedded videos with something like this:
$(document).ready(function() {
$("a.youtube").click(function() {
$.fancybox({
'padding' : 0,
'autoScale' : false,
'transitionIn' : 'none',
'transitionOut' : 'none',
'title' : this.title,
'width' : 680,
'height' : 495,
'href' : this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
'type' : 'swf', // <--add a comma here
'swf' : {'allowfullscreen':'true','wmode':'opaque'}
});
return false;
});
Same thing can be done with Vimeo urls.

Related

how to convert every youtube link i have on my wordpress posts into embed?

i am using the latest version of wordpress , i have around 10000 post right now on my website.
i have a nice plugin that makes my website looks a bit more fancey but i need to ad a simple code to each featured image and each embeded youtube video i have in my posts (if i could pick user who add it too that would be perfect ! ) to get it to work
i know that youtube embed code is
<iframe width="560" height="315" src="//www.youtube.com/embed/NrH9CqnzzRU?list=PLHPcpp4e3JVpXbtgyD-k-nCmwpbbMIHOh" frameborder="0" allowfullscreen></iframe>
what i need to do is to add
<div class="featuredvideo"><iframe width="560" height="315" src="//xxxxxxxxxxxxxxxxxxxxxxxxx" frameborder="0" allowfullscreen></iframe></div>
so i want to add those things before and and after "XXXX" s
i am not a programer so please give me the code
i know that AJAX can search entire folder with code like
<button>AJAX</button>
<br>
<div id = "container"></div>
<script type="text/javascript">
$("button").click(function(){
$.get("sample.html",function(data){
$("#container").html(data);
})
});
which will just get html from a file but that is now what i want to do here.
please help me , give me a plugin or a code to work with
thank you
To wrap the Youtube iframes with the div as required, just use this...
$(function() {
$("iframe[src*=www.youtube.com]").wrap("<div class=\"featuredvideo\" />");
});
That will run when the document has finished loading. It will find all iframes with the string www.youtube.com in the src attribute and wrap them in the required div.
Obviously, if you add any more iframes to the page later and it doesn't reload then you'll need to run that code again. If you did just that then it would wrap the iframes in the div again, which would be wrong. This will handle running the code multiple times...
function wrapYoutubeEmbeds() {
$("iframe[src*=www.youtube.com]").each(function() {
if (!$(this).parent().hasClass("featuredvideo")) {
$(this).wrap("<div class=\"featuredvideo\" />");
}
});
}
$(function() {
wrapYoutubeEmbeds();
});
It simply checks to see if each iframe is inside something with the class featuredvideo and ignores it if it is.

jquery - changing object attribute after creating instance on document.ready()

i'm using the bxSlider plugin (a slider similar to jcarousel) on custom Magento store.
While the slider is istantiated on documet.ready() (in the head html section) by this way:
jQuery('#slider2').bxSlider({
pause: 4000,
auto: true,
autoControls: true,
displaySlideQty: 4,
moveSlideQty: 1
});
I want to change auto attribute to false in the php code if the slider items count is less than 5. I've tried to change it with:
<?php
// slider items count
$prom_count = $this->getPromotionalProducts()->count();;
?>
<?php if($prom_count<5): ?>
<script>
$("#slider2").attr('auto','false');
</script>
<?php endif; ?>
but it doesn't work, so i'm not sure that attribute of objects instantiated on dom ready can be changed by this way. anybody could help?
The attr-method from the jQuery-library works with attributes for HTML-elements:
<img src="..." alt="...">
In this case, src and alt are "attributes".
The bxSlider is configured by giving the bxSlider-function an array. You can't alter this array after it was passed to the function.
What you can do to toggle the auto-show is using bxSlider's stopShow()-function. To probably do this, you'll want to save the bxSlider-instance you created:
var slider = jQuery('#slider2').bxSlider({
...
});
So you can then call the function on this object:
slider.stopShow();
Doing this with PHP and JavaScript in a mixed way (like you suggested) is ugly and should be avoided.
Instead, you should use a JavaScript-only solution:
var slider = jQuery('#slider2').bxSlider({
auto: true,
...
});
if (slider.getSlideCount() < 5){
slider.stopShow();
}
Note that getSlideCount() returns the number of slides, which is not guarantied to be the number of Images in the gallery! The number of slides displayed at once can be set by the displaySlideQty-attribute. The default however is 1.
It seams that there is a Bug which causes the startShow and stopShow-functions not to work: https://github.com/wandoledzep/bxslider/pull/43
As a dirty workaround, you could do something like this:
<?php
echo "<script type=\"text/javascript\">
jQuery('#slider2').bxSlider({
pause: 4000,
autoControls: true,
displaySlideQty: 4,";
if($prom_count >= 5) echo "auto: true,";
echo "moveSlideQty: 1
});
</script>";
?>
Looks like there is no API function for this. Maybe the config will be loaded again if you use reloadShow(). If that does not help you have to read the source code.
That will set the auto attribute on the element it self, not the auto option of the plugin. It doesn't look like the plugin has the ability to change options after you have instantiated it, although I only had a quick look.
Can you not set the right auto value when you run the plugin by putting the php code first?

close fancybox and open new one using functions

I have an application where I am using Fancyboxes to display content from the server through IFRAMES within the Fancyboxes. So, I am looking to run a script after data is submitted or changed in the server that calls the current fancybox with IFRAME to close and open another fancybox with a new url, size, and iframe inside of it.
I have tried several different codes through various googled sites, but none have worked for my app.
Help would be greatly appreciated!
UPDATE: Found the answer here: http://www.amitpatil.me/fancybox-runtime-resizing-of-iframe/
Call $.fancybox.close or parent.$.fancybox.close() (from iframe), and open a new one:
$.fancybox({
'transitionIn' : 'none',
'transitionOut' : 'none',
'width' : 680,
'height' : 495,
'href' : 'http://example.com/new'
});
or simply resize fancybox: $.fancybox.resize().
This will help you in resizing: How do you resize Fancybox at runtime?
[UPDATE]
As I understand: You have A.php opened with fancybox. On A.php you probably have a form, and the form is submitted to B.php. If this is the situation, you have to close+open OR just resize the fancybox in the document.ready of B.php by parent.$.fancybox.function_name.
Don't know if you've tried this, but seems to work for me:
parent.MyFunction(); // call this from the iframed url
function MyFunction(){ // have this in your parent page
// call your fancybox close function here
}
The method suggested by #TamasPap didn't work for me, and since I'd spent hours researching and experimenting until I finally found a method which worked, I wanted to share it.
parent.$.fancybox.open({ href : 'iframe2.html', type: 'iframe'});
parent.$.fancybox.close({ href : 'iframe1.html'});
You can specify any options, helpers or css after type: 'iframe' in the same manner you would when creating the function.

tinymce getContent() returns empty string when editor loaded in ajax

I am integrating tinyMCE editor along with a system am developing with Zend Framework.
Following is the code for the partial view of tinymce editor code
<script type="text/javascript" src="<?php echo $this->baseUrl()?>/js/tinymce/jquery.tinymce.js"></script>
<script type="text/javascript" src="<?php echo $this->baseUrl()?>/js/jquery.validate.tinymce.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('textarea.editor').tinymce({
script_url : '<?php echo $this->baseUrl()?>/js/tinymce/tiny_mce.js',
// General options
theme : "advanced",
plugins : "pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,advlist",
// Theme options
theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak",
theme_advanced_toolbar_location : "top",
theme_advanced_toolbar_align : "left",
theme_advanced_statusbar_location : "bottom",
theme_advanced_resizing : true
});
});
</script>
The problem is that when the view is loaded in ajax, calling the following on the click event of the submit button:
var content = tinyMCE.get('body').getContent();
alert(content);
return's an empty string. If the view is loaded normally everything works fine. This is only in FF. Chrome and IE8 are working fine even with Ajax.
Does anybody have previous experiences with this?
Edit:
Basically my app is using MVC structure. A view is the V part of the MVC. The main page loaded to browsers is a items-listing page which contains an "Add new" button. Once clicked a new view is loaded via ajax and added to the page. The loaded view contains both the all the javascript code for the tinymce and the textarea HTML as well. Its all working fine except for the getContent() call which is not working on FF
Ran into this myself and the "fix" is a beast. In broad strokes, Tiny stores references to DOM elements (document, body, contentFrame, contentWindow) in its instances. I think (but not 100% sure) that it also ends up creating references to them in closures generated by its internal DOM, Selection, and other utils. Because it does this, when you view changes, the old DOM is blown away and replaced with something new (maybe with identical IDs?), but the internal references are preserved.
The solution I needed to implement to support IEx, FF3.x, WebKit (Chrome & Safari), was to call something like BEFORE calling any code that will redraw the RTE area:
for(var i=0, n=tinymce.editors.length; i<n; i++) {
tinymce.editors[i].destroy();
delete document.getElementById(tinymce.editors[i].id);
}
I am not sure, but i think it might be because of several tinymce instance having the same id.
If you use firebug try this to check this out.
for (var i = 0; i < tinymce.editors.length; i++) {
console.log(tinymce.editors[i].id);
}
If there are id doubled you got the root of your problem.

TinyMCE Draws Okay, But Won't Let Me Type In It. In FF, Buttons Have Error: Component returned failure code: 0x80004005

In previous applications, I was able to get TinyMCE to work just fine. But in this web app, I get the rich editor to show up okay, but for some reason I cannot type into the rich editor field and when I click a button like for bolding, I get this error:
Error: [Exception... "Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIDOMNSHTMLDocument.execCommand]" nsresult: "0x80004005 (NS_ERROR_FAILURE)" location: "JS frame :: http://mysite/tiny_mce/tiny_mce.js :: anonymous :: line 1" data: no]
Source File: http://mysite/tiny_mce/tiny_mce.js
Line: 1
I'd like to know what I can do to debug what's going on here. What could be causing this strange error?
Some background:
This code loads the TinyMCE:
<script type="text/javascript" src="http://mysite/tiny_mce/tiny_mce.js"></script>
<script>
tinyMCE.init({
mode : 'none',
editor_selector: 'mceAdvanced',
theme : 'advanced',
theme_advanced_toolbar_location : 'top',
theme_advanced_toolbar_align : 'left',
theme_advanced_buttons1 : 'fontsizeselect,bold,italic,|,bullist,numlist,|,outdent,indent,|,removeformat',
theme_advanced_buttons2: '',
theme_advanced_buttons3: '',
theme_advanced_font_sizes: "1, 2, 3, 4",
width: '600',
height: '200',
remove_script_host : true,
cleanup_on_startup : true,
cleanup: true,
debug : true,
convert_urls : false
});
tinyMCE.execCommand('mceAddControl', true, 'fldOverview');
</script>
<textarea id="fldOverview" name="fldOverview" class="textbox"><?= OVERVIEW ?></textarea>
Tested on:
FF3 fails. Opera (latest stable) works. Windows IE7 works. Safari (latest stable) works.
The answer is here.
The deal is this. Ever use Facebook? We were trying to implement a similar interface where you click to edit a profile section, it collapses and re-expands with a progress bar, then collapses and re-expands with a profile form. In that profile form, we had the TinyMCE rich editor.
Well, it turns out that there's a quirk with DIVs being hidden and then shown to display the TinyMCE control. It gets the timing off or something? Anyway, we were using the slideToggle API in jQuery to collapse and re-expand a DIV with new contents that we pulled back via jQuery AJAX stuff. And when we did, somehow this slideToggle API hosed us up.
The fix was to do the slideToggle like we normally do, but before we load the tinyMCE editor with the execCommand technique, we need to use the show API in jQuery to ensure our DIV is forced open and visible, first. When we did that, the problem went away.
Are you executing this in Firefox ?
Because according to this, it comes up when you disable popups in firefox because of the way pop up blocking is implemented.
Enable pop ups and you are good to go!

Categories