Welcome Guest, Not a member yet? Register   Sign In
HTML Helper and Javascript issues
#1

[eluser]korukyu[/eluser]
Hi, sorry. Total newb here. I'm new to CI and I was trying to modify the HTML helper by adding a script_tag function. I made a new helper (my_html_helper), and I changed some of the code from the original link_tag function.
Code:
function script_tag($src='', $type = 'text/javascript')
    {
        $CI =& get_instance();

        $script = '<xscript ';
        
        if ( strpos($src, '://') !== FALSE)
            {
                $script .= ' src="'.$src.'" ';
            }
            
            else
            {
                $script .= ' src="'.$CI->config->slash_item('base_url').$src.'" ';
            }
        
        
        $script .= '" type="'.$type.'" ';
        
        $script .= '/>';
    
    }
I added the 'x' in the script tag because the forum eats script tags, apparently. :lol:

Then when I call it, I call
Code:
echo script_tag('js/quiz.js');

Of course, it doesn't work. Confusedhut: I'm sure I'm screwing something up royally, and I thought I'd ask the experts! Any ideas?
#2

[eluser]TheFuzzy0ne[/eluser]
Welcome to the CodeIgniter community.

Please can you be more specific? "It doesn't work", doesn't really help us figure out where the problem might be. Do you get an error? If so, what is it? Do you get an unexpected string returned? If so, what do you get?
#3

[eluser]korukyu[/eluser]
Uh, the controller in which I call the script just gives me a blank page.
#4

[eluser]TheFuzzy0ne[/eluser]
Sounds more like an issue with a whitespace somewhere it shouldn't be. Check the end of your helper file, and remove the closing ?&gt; tag.
#5

[eluser]darkhouse[/eluser]
I think you just need to do return $script; in your helper function. You're not returning anything, therefore there's nothing to echo in your view.
#6

[eluser]Thorpe Obazee[/eluser]
[quote author="darkhouse" date="1236167290"]I think you just need to do return $script; in your helper function. You're not returning anything, therefore there's nothing to echo in your view.[/quote]

:p

and you should replace
Code:
<xscript ';
with
Code:
&lt;script
#7

[eluser]korukyu[/eluser]
okay, I fixed the return (can't believe I missed that...wow.) and removed the ending ?&gt;, which was a little less obvious but still a silly mistake. and the <xscript is just so that the post won't eat the tag and replace it with [removed], like it does when it's in the code box. It's not like that in my actual code %-P .

Now when I load it, all the javascript on the page disappears, including the form to which it's linked...?

Code:
echo script_tag('js/quiz.js');
echo "<div id='main'>";
    
echo '&lt;form&gt;
   &lt;input type="button" value="Click me!"&gt;
   &lt;/form&gt;';

The above button doesn't show up at all, but if I comment out the echo script_tag() call, the button DOES appear. The javascript also appears to be giving me a 404 error. I must be having a URL problem. Confusedhut:
#8

[eluser]pistolPete[/eluser]
How does the generated HTML look like? How should it look like to link to a valid location?
#9

[eluser]TheFuzzy0ne[/eluser]
[url="http://validator.w3.org/"]Validate your output HTML[/url].
#10

[eluser]korukyu[/eluser]
Looks like the problem lies in my terribly convoluted .htaccess or something, since the page can't seem to find the js folder. I am still terribly fuzzy on how on earth CI's URLs work, and I removed the index.php from the URLs through my .htaccess file. And that works fine for controllers and views, but apparently I don't know what I'm doing, since CI/js/javascript.js doesn't work, presumably because it's looking in my controller folder?

Is there a way to just show the server-view of the website, like by adding a function to the main controller that prints out page links? The directory helper only gives me the absolute paths, so even though I call my main controller from localhost/CI/, it will still show up in localhost/CI/application/blahblahblah...




Theme © iAndrew 2016 - Forum software by © MyBB