Welcome Guest, Not a member yet? Register   Sign In
codeigniter javascript phpquery
#1

[eluser]joeizang[/eluser]
hi y'all,

I am building an application and I have need to use the base_url() function or site_url() in javasript with my codeigniter so I don't have to hardcode links in. I have come across a lot of solutions that seem to be messy. One of codeigniter's strong points that attracted me is the simplicity and cleanliness of code solutions. So I ran into a problem where I wanted to change a header image that I set with CSS but with javascript it gets hicky up and all that so I wanted to know if I can use phpquery to stuff like jquery? And if so how do we make it a library to use normally like native codeigniter libraries?
#2

[eluser]CroNiX[/eluser]
In the head of your document, before you load any of your own js files:
Code:
<skript type="text/javascript">
  var base_url = "&lt;?php echo base_url(); ?&gt;";
  var site_url = "&lt;?php echo site_url(); ?&gt;";
</skript>
Spelling script correctly, of course.

Now, base_url and site_url are available to any javascript...

For images in css, I just use:

Code:
background-image:url('/images/paper.gif');
which is really the same as
http://yoursite.com/images/paper.gif

Another advantage to that is it works on pages with or without ssl without having to adjust it. It will just assume the current protocol.
#3

[eluser]joeizang[/eluser]
Thanks croNix, I will try this and let you know.
#4

[eluser]joeizang[/eluser]
CroNix,

Thanks for the tip cos it pointed me in the right direction. I am using the template library created by phil sturgeon so I had to do add this bit of code to make it work:

Code:
$this->template->append_metadata('[removed]var base_url = '.base_url().'[removed]');

This had the effect that you spoke of. Very well played sir.

Thank you
#5

[eluser]NeoArc[/eluser]
If you are testing stuff in shared servers, you may want to use relative paths in the css files:

background-image: url("../img/demo.css");

http://css-tricks.com/quick-reminder-about-file-paths/

This is useful when the top directory is probably not the same as your domain root directory (development && testing environments).
#6

[eluser]InsiteFX[/eluser]
Simple fix for inline images.
Code:
&lt;head&gt;
&lt;base href="&lt;?php echo base_url();?&gt;" /&gt;
&lt;/head&gt;
#7

[eluser]NeoArc[/eluser]
In PyroCMS they use BASE_URI, a 'bandwidth-friendly' constant.

Code:
//constants.php (old code??)
if(isset($_SERVER['HTTP_HOST']))
{
        $base_url = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ? 'https' : 'http';
        $base_url .= '://'. $_SERVER['HTTP_HOST'];
        $base_url .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
        
        // Base URI (It's different to base URL!)
        $base_uri = parse_url($base_url, PHP_URL_PATH);
        if(substr($base_uri, 0, 1) != '/') $base_uri = '/'.$base_uri;
        if(substr($base_uri, -1, 1) != '/') $base_uri .= '/';
}

else
{
        $base_url = 'http://localhost/';
        $base_uri = '/';
}

// Define these values to be used later on
define('BASE_URL', $base_url);
define('BASE_URI', $base_uri);
define('APPPATH_URI', BASE_URI.APPPATH);

// We dont need these variables any more
unset($base_uri, $base_url);
define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');




Theme © iAndrew 2016 - Forum software by © MyBB